-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
6,964 additions
and
434 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# example | ||
|
||
This example demonstrates top level discriminators using `oneOf` | ||
|
||
## Install | ||
|
||
```shell | ||
npm run deps && npm i | ||
``` | ||
|
||
## Run | ||
|
||
From this `7-schema-object-mapper` directory, run: | ||
|
||
```shell | ||
npm start | ||
``` | ||
|
||
## Try | ||
|
||
### Discriminator with explict mapping | ||
|
||
#### `"pet_type": "cat" | ||
|
||
```shell | ||
curl -X POST 'http://localhost:3000/v1/pets/mapping' \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{"age": 10, "hunts": true, "pet_type": "cat"}' | ||
{ | ||
"age": 10, | ||
"hunts": true, | ||
"pet_type": "cat" | ||
} | ||
``` | ||
#### `"pet_type": "dog" | ||
|
||
```shell | ||
curl -X POST 'http://localhost:3000/v1/pets/mapping' \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{"bark": true, "breed": "Retriever", "pet_type": "dog"}' | ||
{ | ||
"bark": true, | ||
"breed": "Retriever", | ||
"pet_type": "dog" | ||
} | ||
``` | ||
|
||
### Discriminator with implict mapping | ||
|
||
#### `"pet_type": "DogObject" | ||
|
||
```shell | ||
curl -X POST 'http://localhost:3000/v1/pets/nomapping' \ | ||
-H 'Content-Type: application/json' \ | ||
-d '{"bark": true, "breed": "Retriever", "pet_type": "DogObject"}' | ||
{ | ||
"bark": true, | ||
"breed": "Retriever", | ||
"pet_type": "DogObject" | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
openapi: '3.0.3' | ||
info: | ||
version: 1.0.0 | ||
title: Swagger | ||
servers: | ||
- url: /v1 | ||
paths: | ||
/pets/nomapping: | ||
post: | ||
description: Creates a new pet in the store. | ||
requestBody: | ||
description: Pet to add to the store | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
oneOf: | ||
- $ref: '#/components/schemas/CatObject' | ||
- $ref: '#/components/schemas/DogObject' | ||
discriminator: | ||
propertyName: pet_type | ||
responses: | ||
'200': | ||
description: Updated | ||
|
||
/pets/mapping: | ||
post: | ||
description: Creates a new pet in the store. | ||
requestBody: | ||
description: Pet to add to the store | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
oneOf: | ||
- $ref: '#/components/schemas/CatObject' | ||
- $ref: '#/components/schemas/DogObject' | ||
discriminator: | ||
propertyName: pet_type | ||
mapping: | ||
cat: '#/components/schemas/CatObject' | ||
kitty: '#/components/schemas/CatObject' | ||
dog: '#/components/schemas/DogObject' | ||
puppy: '#/components/schemas/DogObject' | ||
responses: | ||
'200': | ||
description: Updated | ||
|
||
components: | ||
schemas: | ||
DogObject: | ||
type: object | ||
required: | ||
- bark | ||
- breed | ||
- pet_type | ||
properties: | ||
bark: | ||
type: boolean | ||
breed: | ||
type: string | ||
enum: [Dingo, Husky, Retriever, Shepherd] | ||
pet_type: | ||
type: string | ||
# since we use an enum here | ||
# add DogObject as an option | ||
# so that the non-mapping / implied mapping tests can pass | ||
enum: [dog, puppy, DogObject] | ||
|
||
CatObject: | ||
type: object | ||
required: | ||
- hunts | ||
- age | ||
- pet_type | ||
properties: | ||
hunts: | ||
type: boolean | ||
age: | ||
type: integer | ||
pet_type: | ||
type: string | ||
# since we use an enum here | ||
# add CatObject as an option | ||
# so that the non-mapping / implied mapping tests can pass | ||
enum: [cat, kitty, CatObject] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const bodyParser = require('body-parser'); | ||
const http = require('http'); | ||
const OpenApiValidator = require('express-openapi-validator'); | ||
|
||
const port = 3000; | ||
const app = express(); | ||
const apiSpec = path.join(__dirname, 'api.yaml'); | ||
|
||
// 1. Install bodyParsers for the request types your API will support | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(bodyParser.text()); | ||
app.use(bodyParser.json()); | ||
|
||
// Optionally serve the API spec | ||
app.use('/spec', express.static(apiSpec)); | ||
|
||
// 2. Install the OpenApiValidator on your express app | ||
app.use( | ||
OpenApiValidator.middleware({ | ||
apiSpec, | ||
}), | ||
); | ||
// 3. Add routes | ||
app.post(`/v1/pets/mapping`, (req, res) => { | ||
console.log('here', req.body); | ||
res.json(req.body); | ||
}); | ||
app.post(`/v1/pets/nomapping`, (req, res) => { | ||
res.json(req.body); | ||
}); | ||
|
||
// 4. Create a custom error handler | ||
app.use((err, req, res, next) => { | ||
// format errors | ||
res.status(err.status || 500).json({ | ||
message: err.message, | ||
errors: err.errors, | ||
}); | ||
}); | ||
|
||
http.createServer(app).listen(port); | ||
console.log(`Listening on port ${port}`); | ||
|
||
module.exports = app; |
Oops, something went wrong.