diff --git a/test/one.of.2.spec.ts b/test/one.of.2.spec.ts new file mode 100644 index 00000000..9b34495f --- /dev/null +++ b/test/one.of.2.spec.ts @@ -0,0 +1,84 @@ +import * as path from 'path'; +import * as express from 'express'; +import { expect } from 'chai'; +import * as request from 'supertest'; +import { createApp } from './common/app'; + +const packageJson = require('../package.json'); + +describe.only(packageJson.name, () => { + let app = null; + + before(async () => { + const apiSpec = path.join('test', 'resources', 'one.of.2.yaml'); + app = await createApp( + { apiSpec }, + 3005, + app => { + app.post(`${app.basePath}/typethrees`, (req, res) => { + res.status(201).json(req.body); + }); + app.use((err, req, res, next) => { + res.status(err.status || 500).json({ + message: err.message, + code: err.status || 500, + }); + }); + }, + false, + ); + }); + + after(() => { + app.server.close(); + }); + it('should POST TypeOne', async () => { + return request(app) + .post(`${app.basePath}/typethrees`) + .send({ + whatever: 'Whatever One', + somethings: [ + { + type: 'TypeOne', + uniqueOne: 'Unique One', + }, + ], + }) + .expect(201) + .expect('Content-Type', /json/) + .then(data => { + expect(data).to.not.be.an('undefined'); + expect(data.body).to.not.be.an('undefined'); + expect(data.body.whatever).to.not.be.an('undefined'); + expect(data.body.whatever).to.equal('Whatever One'); + expect(data.body.somethings).to.not.be.an('undefined'); + expect(data.body.somethings[0].type).to.equal('TypeOne'); + expect(data.body.somethings[0].uniqueOne).to.equal('Unique One'); + }); + }); + + it('should POST TypeTwo', async () => { + return request(app) + .post(`${app.basePath}/typethrees`) + .send({ + whatever: 'Whatever Two', + somethings: [ + { + type: 'TypeTwo', + uniqueTwo: 'Unique Two', + }, + ], + }) + .expect(201) + .expect('Content-Type', /json/) + .then(data => { + expect(data).to.not.be.an('undefined'); + expect(data.body).to.not.be.an('undefined'); + expect(data.body.whatever).to.not.be.an('undefined'); + expect(data.body.whatever).to.equal('Whatever Two'); + expect(data.body.somethings).to.not.be.an('undefined'); + expect(data.body.somethings[0].type).to.equal('TypeTwo'); + expect(data.body.somethings[0].uniqueTwo).to.equal('Unique Two'); + }); + }); +}); diff --git a/test/resources/one.of.2.yaml b/test/resources/one.of.2.yaml new file mode 100644 index 00000000..faf25c47 --- /dev/null +++ b/test/resources/one.of.2.yaml @@ -0,0 +1,140 @@ + +openapi: '3.0.0' + +info: + description: express-openapi-validator-test + version: 1.0.0 + title: express-openapi-validator-test + license: + name: MIT License + url: https://opensource.org/licenses/MIT + +servers: + - url: http://localhost:3010/v0 + +paths: + /typethrees: + post: + requestBody: + required: true + content: + application/json: + schema: + oneOf: + - $ref: '#/components/schemas/TypeThree' + responses: + '201': + description: Application response + content: + application/json: + schema: + $ref: '#/components/schemas/TypeThree' + default: + description: unexpected error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' + +components: + schemas: + Error: + required: + - code + - message + properties: + code: + type: integer + format: int32 + message: + type: string + + SuperTypeOne: + type: object + properties: + type: + type: string + value: + type: number + format: int32 + discriminator: + propertyName: type + required: + - value + - type + + TypeOne: + allOf: + - $ref: '#/components/schemas/SuperTypeOne' + type: object + properties: + type: + type: string + enum: [TypeOne] + value: + type: number + format: int32 + enum: [ 1 ] + default: 1 + uniqueOne: + type: string + default: Unique One + required: + - type + - uniqueOne + + TypeTwo: + allOf: + - $ref: '#/components/schemas/SuperTypeOne' + type: object + properties: + type: + type: string + enum: [TypeTwo] + value: + type: number + format: int32 + enum: [ 2 ] + default: 2 + uniqueTwo: + type: string + default: Unique Two + required: + - uniqueTwo + + SuperTypeTwo: + type: object + properties: + id: + type: string + format: uuid + date: + type: string + format: date-time + someStrings: + type: array + items: + type: string + + TypeThree: + allOf: + - $ref: '#/components/schemas/SuperTypeTwo' + type: object + properties: + type: + type: string + enum: [TypeThree] + default: TypeThree + somethings: + type: array + minItems: 1 + maxItems: 2 + items: + oneOf: + - $ref: "#/components/schemas/TypeOne" + - $ref: "#/components/schemas/TypeTwo" + discriminator: + propertyName: type + required: + - type + - somethings