Skip to content

Commit

Permalink
feat: discriminator example
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Dec 28, 2020
1 parent dd9eb8e commit 95509b8
Show file tree
Hide file tree
Showing 6 changed files with 6,964 additions and 434 deletions.
61 changes: 61 additions & 0 deletions examples/8-top-level-discriminator/README.md
@@ -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"
}
```
86 changes: 86 additions & 0 deletions examples/8-top-level-discriminator/api.yaml
@@ -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]
46 changes: 46 additions & 0 deletions examples/8-top-level-discriminator/app.js
@@ -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;

0 comments on commit 95509b8

Please sign in to comment.