From 53829f18a6c763c59360b27d5667fdd12600e4b6 Mon Sep 17 00:00:00 2001 From: Mahtis Michel Date: Thu, 29 Apr 2021 21:31:22 +0200 Subject: [PATCH] fix: required properties (#7206) * fix: required properties schema required properties should not be treated like required: true. this lead to objects that requires properties being treated as required itself * test: try-it-out-schema-required-override-allowed Co-authored-by: Tim Lai --- src/core/utils.js | 2 +- ...-out-schema-required-override-allowed.yaml | 34 +++++++++++++++++++ ...it-out-schema-required-override-allowed.js | 15 ++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 test/e2e-cypress/static/documents/features/try-it-out-schema-required-override-allowed.yaml create mode 100644 test/e2e-cypress/tests/features/try-it-out-schema-required-override-allowed.js diff --git a/src/core/utils.js b/src/core/utils.js index 814737cb..7a57f369 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -429,7 +429,7 @@ function validateValueBySchema(value, schema, requiredByParam, bypassRequiredChe let minItems = schema.get("minItems") let pattern = schema.get("pattern") - const schemaRequiresValue = requiredByParam || requiredBySchema + const schemaRequiresValue = requiredByParam || requiredBySchema === true const hasValue = value !== undefined && value !== null const isValidEmpty = !schemaRequiresValue && !hasValue diff --git a/test/e2e-cypress/static/documents/features/try-it-out-schema-required-override-allowed.yaml b/test/e2e-cypress/static/documents/features/try-it-out-schema-required-override-allowed.yaml new file mode 100644 index 00000000..b0988db2 --- /dev/null +++ b/test/e2e-cypress/static/documents/features/try-it-out-schema-required-override-allowed.yaml @@ -0,0 +1,34 @@ +swagger: '2.0' +info: + version: '1.0' + title: "schema required properties should not be treated like required: true" +paths: + '/v1/any-path': + put: + summary: lorem + operationId: setDeliveryLocation + produces: + - application/json + parameters: + - in: body + name: body + description: ipsum + required: false + schema: + $ref: '#/definitions/TopModel' + responses: + '200': + description: successful operation +definitions: + TopModel: + type: object + properties: + testProperty: + $ref: '#/definitions/NestedModel' + NestedModel: + type: object + required: + - validated + properties: + validated: + type: boolean diff --git a/test/e2e-cypress/tests/features/try-it-out-schema-required-override-allowed.js b/test/e2e-cypress/tests/features/try-it-out-schema-required-override-allowed.js new file mode 100644 index 00000000..d1a1af04 --- /dev/null +++ b/test/e2e-cypress/tests/features/try-it-out-schema-required-override-allowed.js @@ -0,0 +1,15 @@ +describe("Try It Out: schema required properties can be overriden", () => { + it("should execute", () => { + cy + .visit("?tryItOutEnabled=true&url=/documents/features/try-it-out-schema-required-override-allowed.yaml") + .get("#operations-default-setDeliveryLocation") + .click() + .get(".body-param__text") + .should("include.value", "testProperty") + .clear() // swagger-ui will auto insert "{}" into textarea + .get(".execute-wrapper > .btn") + .click() + .get(".curl-command") + .should("exist") + }) +})