From 87ded996fa0bd73df048762268744f120b38742e Mon Sep 17 00:00:00 2001 From: Tim Lai Date: Fri, 18 Jun 2021 16:49:50 -0700 Subject: [PATCH] fix(sample-gen): oas3 form-data object should generate example (#7355) --- .../plugins/oas3/components/request-body.jsx | 6 ++-- .../request-body/multipart/default-views.yaml | 28 +++++++++++++++++++ .../oas3-request-body-default-views.js | 16 +++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/e2e-cypress/static/documents/features/request-body/multipart/default-views.yaml create mode 100644 test/e2e-cypress/tests/features/oas3-request-body-default-views.js diff --git a/src/core/plugins/oas3/components/request-body.jsx b/src/core/plugins/oas3/components/request-body.jsx index defe4f00e41..fba93741fcb 100644 --- a/src/core/plugins/oas3/components/request-body.jsx +++ b/src/core/plugins/oas3/components/request-body.jsx @@ -165,14 +165,16 @@ const RequestBody = ({ const useInitialValue = useInitialValFromSchemaSamples || useInitialValFromEnum let initialValue = "" - if(type === "array" && !useInitialValue) { + if (type === "array" && !useInitialValue) { initialValue = [] - } else if (useInitialValue) { + } + if (type === "object" || useInitialValue) { // TODO: what about example or examples from requestBody could be passed as exampleOverride initialValue = getSampleSchema(prop, false, { includeWriteOnly: true }) } + if (typeof initialValue !== "string" && type === "object") { initialValue = stringify(initialValue) } diff --git a/test/e2e-cypress/static/documents/features/request-body/multipart/default-views.yaml b/test/e2e-cypress/static/documents/features/request-body/multipart/default-views.yaml new file mode 100644 index 00000000000..5db8f34410e --- /dev/null +++ b/test/e2e-cypress/static/documents/features/request-body/multipart/default-views.yaml @@ -0,0 +1,28 @@ +openapi: 3.0.3 +info: + title: multipart/form-data schema object + version: 0.0.1 +paths: + /test: + post: + requestBody: + content: + multipart/form-data: + schema: + type: object + properties: + file: + type: string + format: binary + parameters: + "$ref": "#/components/schemas/TestBody" + responses: + 200: + description: ok +components: + schemas: + TestBody: + type: object + properties: + stuff: + type: string diff --git a/test/e2e-cypress/tests/features/oas3-request-body-default-views.js b/test/e2e-cypress/tests/features/oas3-request-body-default-views.js new file mode 100644 index 00000000000..d75c86a5e9a --- /dev/null +++ b/test/e2e-cypress/tests/features/oas3-request-body-default-views.js @@ -0,0 +1,16 @@ +describe("OAS3 default views", () => { + describe("multipart/form-data", () => { + it("should display calculated object string, when no examples provided (#7268)", () => { + cy.visit( + "/?url=/documents/features/request-body/multipart/default-views.yaml", + ) + .get("#operations-default-post_test") + .click() + // Expand Try It Out + .get(".try-out__btn") + .click() + .get(".parameters-col_description textarea") + .should("contains.text", "\"stuff\": \"string\"") + }) + }) +})