Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(oas3): add support for oneOf/anyOf JSON Schema keywords in request-body rendering #9767

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions src/core/plugins/oas3/components/request-body.jsx
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ components:
type: string
default: ''
maxLength: 64
example: MyDocument.pdf'
example: NestedSchemaExample.pdf'
category:
type: string
pattern: ''
Expand Down Expand Up @@ -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'
Expand Down