diff --git a/.travis.yml b/.travis.yml index 2bb53086..3d8e26f0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ sudo: required language: node_js node_js: -- '15' - '14' - '12' - '10' diff --git a/src/middlewares/parsers/request.schema.preprocessor.ts b/src/middlewares/parsers/request.schema.preprocessor.ts index b25cbb24..4a216dbb 100644 --- a/src/middlewares/parsers/request.schema.preprocessor.ts +++ b/src/middlewares/parsers/request.schema.preprocessor.ts @@ -82,6 +82,9 @@ export class RequestSchemaPreprocessor { schema.required = required .slice(0, index) .concat(required.slice(index + 1)); + if (schema.required.length === 0) { + delete schema.required; + } } } }; diff --git a/test/oneof.readonly.spec.ts b/test/oneof.readonly.spec.ts index 83aad5ed..da0fe5bb 100644 --- a/test/oneof.readonly.spec.ts +++ b/test/oneof.readonly.spec.ts @@ -14,6 +14,9 @@ describe('one.of readonly', () => { app.use( express .Router() + .post(`${app.basePath}/any_of_one_required`, (req, res) => + res.status(200).json({ success: true }), + ) .post(`${app.basePath}/any_of`, (req, res) => res.status(200).json({ success: true }), ) @@ -46,6 +49,13 @@ describe('one.of readonly', () => { expect(error.message).to.include('to one of the allowed values: C, D'); })); + it('post type anyof without providing the single required readonly property should pass', async () => + request(app) + .post(`${app.basePath}/one_of`) + .send({ type: 'C' }) // do not provide id + .set('Content-Type', 'application/json') + .expect(200)); + it('post type oneOf (without readonly id) should pass', async () => request(app) .post(`${app.basePath}/one_of`) diff --git a/test/oneof.readonly.yaml b/test/oneof.readonly.yaml index 7f7590d2..c4574421 100644 --- a/test/oneof.readonly.yaml +++ b/test/oneof.readonly.yaml @@ -6,6 +6,23 @@ info: servers: - url: /v1 paths: + /any_of_one_required: + post: + requestBody: + content: + application/json: + schema: + anyOf: + - $ref: '#/components/schemas/subE' + - $ref: '#/components/schemas/subF' + discriminator: + propertyName: type + mapping: + A: '#/components/schemas/subE' + B: '#/components/schemas/subF' + responses: + 200: + description: successful operation /any_of: post: requestBody: @@ -57,6 +74,18 @@ components: - id - type + one_required: + type: object + properties: + id: + readOnly: true + type: string + type: + type: string + enum: [A, B] + required: + - id + subA: allOf: - $ref: '#/components/schemas/common' @@ -91,4 +120,12 @@ components: enum: [C, D] required: - id - - type \ No newline at end of file + - type + + subE: + allOf: + - $ref: '#/components/schemas/one_required' + + subF: + allOf: + - $ref: '#/components/schemas/one_required' \ No newline at end of file