From ed983eb26034c1d4b4cdbc672303d27bb3f28109 Mon Sep 17 00:00:00 2001 From: Oliwia Rogala Date: Fri, 29 Mar 2024 14:08:48 +0100 Subject: [PATCH] fix(oas3): add support for oneOf/anyOf JSON Schema keywords in request-body rendering (#9767) Refs #9763 --- .../plugins/oas3/components/request-body.jsx | 23 ++++--------------- .../plugins/oas3/one-of-any-of-example.cy.js | 4 ++-- .../features/oas3-one-of-any-of-example.yaml | 11 ++++++++- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/core/plugins/oas3/components/request-body.jsx b/src/core/plugins/oas3/components/request-body.jsx index 749f861f0e6..7c9799080a6 100644 --- a/src/core/plugins/oas3/components/request-body.jsx +++ b/src/core/plugins/oas3/components/request-body.jsx @@ -1,7 +1,7 @@ import React from "react" import PropTypes from "prop-types" import ImPropTypes from "react-immutable-proptypes" -import { Map, OrderedMap, List } from "immutable" +import { Map, OrderedMap, List, fromJS } from "immutable" import { getCommonExtensions, stringify, isEmptyValue } from "core/utils" import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse" @@ -153,24 +153,9 @@ const RequestBody = ({ Map.isMap(bodyProperties) && bodyProperties.entrySeq().map(([key, schema]) => { if (schema.get("readOnly")) return - const schemaWithoutKeywords = schema.filter((v, k) => k !== "oneOf" - && k !== "anyOf" - && k !== "$$ref" - ) - - if (schemaWithoutKeywords.size === 0) { - const oneOf = schema.get("oneOf") - const anyOf = schema.get("anyOf") - const nestedSchema = List.isList(oneOf) - ? oneOf.get(0) - : List.isList(anyOf) - ? anyOf.get(0) - : null - - if (Map.isMap(nestedSchema)) { - schema = nestedSchema - } - } + const oneOf = schema.get("oneOf")?.get(0)?.toJS() + const anyOf = schema.get("anyOf")?.get(0)?.toJS() + schema = fromJS(fn.mergeJsonSchema(schema.toJS(), oneOf ?? anyOf ?? {})) let commonExt = showCommonExtensions ? getCommonExtensions(schema) : null const required = schemaForMediaType.get("required", List()).includes(key) diff --git a/test/e2e-cypress/e2e/features/plugins/oas3/one-of-any-of-example.cy.js b/test/e2e-cypress/e2e/features/plugins/oas3/one-of-any-of-example.cy.js index 357121ac146..ccd02cdb2b8 100644 --- a/test/e2e-cypress/e2e/features/plugins/oas3/one-of-any-of-example.cy.js +++ b/test/e2e-cypress/e2e/features/plugins/oas3/one-of-any-of-example.cy.js @@ -11,7 +11,7 @@ describe("OpenAPI 3.0 oneOf and anyOf example", () => { .get(".try-out__btn") .click() .get("textarea") - .contains("documentDate") + .contains("NestedSchemaExample.pdf") .should("exist") cy.contains("/documentsWithCombineOneOf").click() cy.contains("/documentsWithCombineAnyOf") @@ -20,7 +20,7 @@ describe("OpenAPI 3.0 oneOf and anyOf example", () => { .contains("Try it out") .click() .get("textarea") - .contains("documentDate") + .contains("ParentSchemaExample.pdf") .should("exist") } ) diff --git a/test/e2e-cypress/static/documents/features/oas3-one-of-any-of-example.yaml b/test/e2e-cypress/static/documents/features/oas3-one-of-any-of-example.yaml index 9adbbd9c5ba..32e93c51ce3 100644 --- a/test/e2e-cypress/static/documents/features/oas3-one-of-any-of-example.yaml +++ b/test/e2e-cypress/static/documents/features/oas3-one-of-any-of-example.yaml @@ -51,7 +51,7 @@ components: type: string default: '' maxLength: 64 - example: MyDocument.pdf' + example: NestedSchemaExample.pdf' category: type: string pattern: '' @@ -172,6 +172,15 @@ components: - $ref: '#/components/schemas/ModelPostDocumentPolicy' - $ref: '#/components/schemas/ModelPostDocumentCustomer' - $ref: '#/components/schemas/ModelPostDocumentInvoice' + example: + title: ParentSchemaExample.pdf + category: Policy + type: Policy + issuingApplicationId: App1 + documentDate: '2021-01-01' + sharedWith: + - CustomerPortal + policyNumber: '132456' ModelPostDocumentAnyOfCombineModelFile: allOf: - $ref: '#/components/schemas/ModelFile'