From d47a825279a3a11186c8ca0e847cd6a00e548e91 Mon Sep 17 00:00:00 2001 From: Carmine DiMascio Date: Sun, 10 Jan 2021 20:16:35 -0500 Subject: [PATCH] test: add polymorphism test for #511 --- .../parsers/schema.preprocessor.ts | 2 +- test/511.spec.ts | 155 ++++++++++++++++++ 2 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 test/511.spec.ts diff --git a/src/middlewares/parsers/schema.preprocessor.ts b/src/middlewares/parsers/schema.preprocessor.ts index 52ce50af..196ccd9e 100644 --- a/src/middlewares/parsers/schema.preprocessor.ts +++ b/src/middlewares/parsers/schema.preprocessor.ts @@ -259,7 +259,7 @@ export class SchemaPreprocessor { const options = opts[kind]; options.path = node.path; - if (nschema) { + if (nschema) { // This null check should no longer be necessary this.handleSerDes(pschema, nschema, options); this.handleReadonly(pschema, nschema, options); this.processDiscriminator(pschema, nschema, options); diff --git a/test/511.spec.ts b/test/511.spec.ts new file mode 100644 index 00000000..4d18b07f --- /dev/null +++ b/test/511.spec.ts @@ -0,0 +1,155 @@ +import * as request from 'supertest'; +import { createApp } from './common/app'; + +describe('511 schema.preprocessor inheritance', () => { + let app = null; + + before(async () => { + // set up express app + app = await createApp( + { + apiSpec: apiSpec(), + validateResponses: true, + }, + 3001, + (app) => { + app.post('/example', (req, res) => { + res.status(201).json({ + object_type: 'PolyObject1', + shared_prop1: 'sp1', + shared_prop2: 'sp2', + polyObject1SpecificProp1: 'poly1', + }); + }); + }, + false, + ); + return app; + }); + + after(() => { + app.server.close(); + }); + + it('should return 201', async () => + request(app) + .post(`/example`) + .send({ + object_type: 'PolyObject1', + shared_prop1: 'sp1', + shared_prop2: 'sp2', + polyObject1SpecificProp1: 'poly1', + }) + .expect(201)); +}); + +function apiSpec(): any { + return { + openapi: '3.0.0', + info: { + title: 'Example API', + version: '0.1.0', + }, + servers: [ + { + url: 'https://localhost/', + }, + ], + paths: { + '/example': { + post: { + description: 'Request', + requestBody: { + content: { + 'application/json': { + schema: { + $ref: '#/components/schemas/PolyObject', + }, + }, + }, + }, + responses: { + '201': { + description: 'Response', + content: { + 'application/json': { + schema: { + type: 'object', + }, + }, + }, + }, + }, + }, + }, + }, + components: { + schemas: { + PolyObject: { + type: 'object', + discriminator: { + propertyName: 'object_type', + mapping: { + PolyObject1: '#/components/schemas/PolyObject1', + PolyObject2: '#/components/schemas/PolyObject2', + }, + }, + oneOf: [ + { + $ref: '#/components/schemas/PolyObject1', + }, + { + $ref: '#/components/schemas/PolyObject2', + }, + ], + }, + PolyObjectBase: { + type: 'object', + required: ['object_type'], + properties: { + object_type: { + type: 'string', + enum: ['PolyObject1', 'PolyObject2'], + }, + shared_prop1: { + type: 'string', + }, + shared_prop2: { + type: 'string', + }, + }, + }, + PolyObject1: { + allOf: [ + { + $ref: '#/components/schemas/PolyObjectBase', + }, + { + type: 'object', + properties: { + polyObject1SpecificProp1: { + type: 'string', + }, + }, + }, + ], + }, + PolyObject2: { + allOf: [ + { + $ref: '#/components/schemas/PolyObjectBase', + }, + { + type: 'object', + properties: { + polyObject2SpecificProp1: { + type: 'string', + }, + }, + }, + ], + }, + }, + }, + }; +}