From 050fd6b47f6ed1e83fb5defec41ab61ddca11590 Mon Sep 17 00:00:00 2001 From: mathis-m Date: Sun, 20 Dec 2020 17:37:44 +0100 Subject: [PATCH 1/2] fix: request body.jsx should rely on the getSampleSchema utility --- .../plugins/oas3/components/request-body.jsx | 63 +++++++++---------- 1 file changed, 29 insertions(+), 34 deletions(-) diff --git a/src/core/plugins/oas3/components/request-body.jsx b/src/core/plugins/oas3/components/request-body.jsx index aa5cefdcfb4..0affb8ac85c 100644 --- a/src/core/plugins/oas3/components/request-body.jsx +++ b/src/core/plugins/oas3/components/request-body.jsx @@ -5,31 +5,28 @@ import { Map, OrderedMap, List } from "immutable" import { getCommonExtensions, getSampleSchema, stringify, isEmptyValue } from "core/utils" function getDefaultRequestBodyValue(requestBody, mediaType, activeExamplesKey) { - let mediaTypeValue = requestBody.getIn(["content", mediaType]) - let schema = mediaTypeValue.get("schema").toJS() - let example = - mediaTypeValue.get("example") !== undefined - ? stringify(mediaTypeValue.get("example")) - : null - let currentExamplesValue = mediaTypeValue.getIn([ - "examples", - activeExamplesKey, - "value" - ]) + const mediaTypeValue = requestBody.getIn(["content", mediaType]) + const schema = mediaTypeValue.get("schema").toJS() - if (mediaTypeValue.get("examples")) { - // the media type DOES have examples - return stringify(currentExamplesValue) || "" - } else { - // the media type DOES NOT have examples - return stringify( - example || - getSampleSchema(schema, mediaType, { - includeWriteOnly: true - }) || - "" - ) - } + const hasExamplesKey = mediaTypeValue.get("examples") !== undefined + const exampleSchema = mediaTypeValue.get("example") + const mediaTypeExample = hasExamplesKey + ? mediaTypeValue.getIn([ + "examples", + activeExamplesKey, + "value" + ]) + : exampleSchema + + const exampleValue = getSampleSchema( + schema, + mediaType, + { + includeWriteOnly: true + }, + mediaTypeExample + ) + return stringify(exampleValue) } @@ -212,6 +209,12 @@ const RequestBody = ({ } + const sampleRequestBody = getDefaultRequestBodyValue( + requestBody, + contentType, + activeExamplesKey, + ) + return
{ requestBodyDescription && @@ -235,11 +238,7 @@ const RequestBody = ({ @@ -257,11 +256,7 @@ const RequestBody = ({ } includeWriteOnly={true} From e7dcdb38612e15fc39fc40d28bb20063afede7e2 Mon Sep 17 00:00:00 2001 From: mathis-m Date: Sun, 20 Dec 2020 18:46:16 +0100 Subject: [PATCH 2/2] test: add test to ensure xml request body --- .../static/documents/bugs/6475.yaml | 57 ++++++++++++++++ test/e2e-cypress/tests/bugs/6475.js | 65 +++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 test/e2e-cypress/static/documents/bugs/6475.yaml create mode 100644 test/e2e-cypress/tests/bugs/6475.js diff --git a/test/e2e-cypress/static/documents/bugs/6475.yaml b/test/e2e-cypress/static/documents/bugs/6475.yaml new file mode 100644 index 00000000000..1d49c9afbf1 --- /dev/null +++ b/test/e2e-cypress/static/documents/bugs/6475.yaml @@ -0,0 +1,57 @@ +openapi: 3.0.1 +info: + title: Example Swagger + version: '1.0' +servers: + - url: /api/v1 +paths: + /xmlTest/examples: + post: + summary: sample issues + operationId: xmlTest_examples + parameters: [] + requestBody: + description: Simple Test xml examples + content: + application/xml: + schema: + $ref: "#/components/schemas/Test" + examples: + test: + value: + x: should be xml + responses: + '200': + description: Simple Test xml examples + content: {} + /xmlTest/example: + post: + summary: sample issues + operationId: xmlTest_example + parameters: [] + requestBody: + description: Simple Test xml example + content: + application/xml: + schema: + $ref: "#/components/schemas/Test" + example: + x: should be xml + responses: + '200': + description: Simple Test xml example + content: {} +components: + schemas: + Test: + type: object + xml: + name: root + properties: + x: + type: string + other: + type: string + format: email + example: + x: what the f diff --git a/test/e2e-cypress/tests/bugs/6475.js b/test/e2e-cypress/tests/bugs/6475.js new file mode 100644 index 00000000000..05fade8003b --- /dev/null +++ b/test/e2e-cypress/tests/bugs/6475.js @@ -0,0 +1,65 @@ +describe("#6475: 'Examples' keyword definitions can not be rendered as xml", () => { + it("should render requestBody examples preview accourdingly to content-type xml", () => { + const xmlIndicator = "should be xml" + + cy + .visit("?url=/documents/bugs/6475.yaml") + .get("#operations-default-xmlTest_examples") + .click() + .get(".opblock-section-request-body") + .within(() => { + cy + .get(".microlight") + .should("include.text", xmlIndicator) + }) + }) + it("should requestBody examples input accourdingly to content-type xml", () => { + const xmlIndicator = "should be xml" + + cy + .visit("?url=/documents/bugs/6475.yaml") + .get("#operations-default-xmlTest_examples") + .click() + .get(".btn.try-out__btn") + .click() + .get(".opblock-section-request-body") + .within(() => { + cy + .get("textarea") + .contains(xmlIndicator) + }) + }) +}) + +describe("#6475: 'Example' keyword definitions can not be rendered as xml", () => { + it("should render requestBody examples preview accourdingly to content-type xml", () => { + const xmlIndicator = "should be xml" + + cy + .visit("?url=/documents/bugs/6475.yaml") + .get("#operations-default-xmlTest_example") + .click() + .get(".opblock-section-request-body") + .within(() => { + cy + .get(".microlight") + .should("include.text", xmlIndicator) + }) + }) + it("should requestBody examples input accourdingly to content-type xml", () => { + const xmlIndicator = "should be xml" + + cy + .visit("?url=/documents/bugs/6475.yaml") + .get("#operations-default-xmlTest_example") + .click() + .get(".btn.try-out__btn") + .click() + .get(".opblock-section-request-body") + .within(() => { + cy + .get("textarea") + .contains(xmlIndicator) + }) + }) +})