From 68bd61a6826307d102b80e0faa50b6f1da60285f Mon Sep 17 00:00:00 2001 From: Mahtis Michel Date: Wed, 3 Mar 2021 21:49:56 +0100 Subject: [PATCH] fix: multipart enum initial value not set (#7004) --- .../plugins/oas3/components/request-body.jsx | 13 +++++++-- .../features/request-body/multipart/enum.yaml | 29 +++++++++++++++++++ .../oas3-user-edit-request-body-flows.js | 25 ++++++++++++++-- 3 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 test/e2e-cypress/static/documents/features/request-body/multipart/enum.yaml diff --git a/src/core/plugins/oas3/components/request-body.jsx b/src/core/plugins/oas3/components/request-body.jsx index db9f8aab..530abfc2 100644 --- a/src/core/plugins/oas3/components/request-body.jsx +++ b/src/core/plugins/oas3/components/request-body.jsx @@ -155,11 +155,18 @@ const RequestBody = ({ const currentValue = requestBodyValue.getIn([key, "value"]) const currentErrors = requestBodyValue.getIn([key, "errors"]) || requestBodyErrors const included = requestBodyInclusionSetting.get(key) || false - let hasNonEmptyInitialVal = prop.has("default") || prop.has("example") || prop.hasIn(["items", "example"]) || prop.hasIn(["items", "default"]) || prop.has("enum") && prop.get("enum").size === 1 + + const useInitialValFromSchemaSamples = prop.has("default") + || prop.has("example") + || prop.hasIn(["items", "example"]) + || prop.hasIn(["items", "default"]) + const useInitialValFromEnum = prop.has("enum") && (prop.get("enum").size === 1 || required) + const useInitialValue = useInitialValFromSchemaSamples || useInitialValFromEnum + let initialValue = "" - if(type === "array" && !hasNonEmptyInitialVal) { + if(type === "array" && !useInitialValue) { initialValue = [] - } else if (hasNonEmptyInitialVal) { + } else if (useInitialValue) { // TODO: what about example or examples from requestBody could be passed as exampleOverride initialValue = getSampleSchema(prop, false, { includeWriteOnly: true diff --git a/test/e2e-cypress/static/documents/features/request-body/multipart/enum.yaml b/test/e2e-cypress/static/documents/features/request-body/multipart/enum.yaml new file mode 100644 index 00000000..cc935a1f --- /dev/null +++ b/test/e2e-cypress/static/documents/features/request-body/multipart/enum.yaml @@ -0,0 +1,29 @@ +openapi: 3.0.3 +info: + title: asd + version: 0.0.1 +paths: + /test: + post: + requestBody: + content: + multipart/form-data: + schema: + $ref: '#/components/schemas/TestBody' + responses: + 200: + description: ok +components: + schemas: + TestBody: + required: + - test_enum + type: object + properties: + test_enum: + allOf: + - $ref: "#/components/schemas/TestEnum" + TestEnum: + enum: + - A + - B diff --git a/test/e2e-cypress/tests/features/oas3-user-edit-request-body-flows.js b/test/e2e-cypress/tests/features/oas3-user-edit-request-body-flows.js index ec66f647..f0fea98f 100644 --- a/test/e2e-cypress/tests/features/oas3-user-edit-request-body-flows.js +++ b/test/e2e-cypress/tests/features/oas3-user-edit-request-body-flows.js @@ -1,12 +1,16 @@ -const getRequestBodyFromCY = (page = null) => - (page || cy.visit( +function getExpandedTryout(page = null, operationId = "#operations-pet-addPet") { + return (page || cy.visit( "/?url=/documents/features/petstore-only-pet.openapi.yaml", )) - .get("#operations-pet-addPet") + .get(operationId) .click() // Expand Try It Out .get(".try-out__btn") .click() +} + +const getRequestBodyFromCY = (page = null, operationId = "#operations-pet-addPet") => + getExpandedTryout(page, operationId) // get textarea .get(".opblock-body .opblock-section .opblock-section-request-body .body-param textarea") @@ -84,4 +88,19 @@ describe("OAS3 Request Body user edit flows", () => { }) }) }) + describe("multipart/", () => { + // Case: User wants to execute operation with media-type multipart/ with a enum property. The user expects the first enum value to be used when execuded. + it("should use the first enum value on execute if not changed by user (#6976)", () => { + // test/e2e-cypress/static/documents/features/request-body/multipart/enum.yaml + getExpandedTryout( + cy.visit( + "/?url=/documents/features/request-body/multipart/enum.yaml", + ), "#operations-default-post_test") + .get(".execute") + .click() + // Assert on the request URL + .get(".curl") + .contains("test_enum=A") + }) + }) })