Skip to content

Commit

Permalink
fix(components): add support for oneOf/anyOf JSON Schema keywords in …
Browse files Browse the repository at this point in the history
…parameter-row rendering (#9934)

Refs #7912
  • Loading branch information
glowcloud committed May 14, 2024
1 parent dcc87aa commit 9037acf
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/core/components/parameter-row.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from "react"
import { Map, List } from "immutable"
import { Map, List, fromJS } from "immutable"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import win from "core/window"
Expand Down Expand Up @@ -97,7 +97,7 @@ export default class ParameterRow extends Component {
let { specSelectors, pathMethod, rawParam, oas3Selectors, fn } = this.props

const paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
const { schema } = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
let { schema } = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
const parameterMediaType = paramWithMeta
.get("content", Map())
.keySeq()
Expand Down Expand Up @@ -126,6 +126,8 @@ export default class ParameterRow extends Component {
? paramWithMeta.getIn(["schema", "example"])
: (schema && schema.getIn(["default"]))
} else if (specSelectors.isOAS3()) {
schema = this.composeJsonSchema(schema)

const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey())
initialValue =
paramWithMeta.getIn(["examples", currentExampleKey, "value"]) !== undefined
Expand Down Expand Up @@ -181,6 +183,13 @@ export default class ParameterRow extends Component {
return `${param.get("name")}-${param.get("in")}`
}

composeJsonSchema(schema) {
const { fn } = this.props
const oneOf = schema.get("oneOf")?.get(0)?.toJS()
const anyOf = schema.get("anyOf")?.get(0)?.toJS()
return fromJS(fn.mergeJsonSchema(schema.toJS(), oneOf ?? anyOf ?? {}))
}

render() {
let {param, rawParam, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath, oas3Selectors} = this.props

Expand Down Expand Up @@ -222,6 +231,10 @@ export default class ParameterRow extends Component {
let { schema } = getParameterSchema(param, { isOAS3 })
let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()

if (isOAS3) {
schema = this.composeJsonSchema(schema)
}

let format = schema ? schema.get("format") : null
let type = schema ? schema.get("type") : null
let itemType = schema ? schema.getIn(["items", "type"]) : null
Expand Down
49 changes: 49 additions & 0 deletions test/e2e-cypress/e2e/features/parameters-one-of-any-of.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @prettier
*/

describe("Parameter with oneOf and anyOf keywords in OpenAPI 3.0.x", () => {
it("should render correct form fields", () => {
cy.visit("/?url=/documents/features/parameters-one-of-any-of-oas3.yaml")
.get("#operations-default-get_")
.click()
cy.get(".parameters-col_description")
.eq(1)
.find("select")
.should("exist")
.and("have.value", "ascending")
cy.get(".parameters-col_description")
.eq(2)
.find("input")
.should("exist")
.and("have.value", "test")
cy.get(".parameters-col_description")
.eq(3)
.find("textarea")
.should("exist")
.and("contain", "\"eq\": \"active\"")
})
})

describe("Parameter with oneOf and anyOf keywords in OpenAPI 3.1.0.", () => {
it("should render correct form fields", () => {
cy.visit("/?url=/documents/features/parameters-one-of-any-of-oas31.yaml")
.get("#operations-default-get_")
.click()
cy.get(".parameters-col_description")
.eq(1)
.find("select")
.should("exist")
.and("have.value", "ascending")
cy.get(".parameters-col_description")
.eq(2)
.find("input")
.should("exist")
.and("have.value", "test")
cy.get(".parameters-col_description")
.eq(3)
.find("textarea")
.should("exist")
.and("contain", "\"eq\": \"active\"")
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openapi: 3.0.0
paths:
/:
get:
parameters:
- name: enum
in: query
schema:
oneOf:
- type: string
default: ascending
enum:
- ascending
- descending
- name: string
in: query
default: test
schema:
anyOf:
- type: string
- name: object
in: query
schema:
oneOf:
- type: object
properties:
eq:
type: string
enum:
- active
- archived
neq:
type: string
enum:
- active
- archived
in:
type: array
items:
type: string
enum:
- active
- archived
notIn:
type: array
items:
type: string
enum:
- active
- archived
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
openapi: 3.1.0
paths:
/:
get:
parameters:
- name: enum
in: query
schema:
oneOf:
- type: string
default: ascending
enum:
- ascending
- descending
- name: string
in: query
default: test
schema:
anyOf:
- type: string
- name: object
in: query
schema:
oneOf:
- type: object
properties:
eq:
type: string
enum:
- active
- archived
neq:
type: string
enum:
- active
- archived
in:
type: array
items:
type: string
enum:
- active
- archived
notIn:
type: array
items:
type: string
enum:
- active
- archived
2 changes: 2 additions & 0 deletions test/unit/bugs/4557-default-parameter-values.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ParameterRow from "core/components/parameter-row"
import {
memoizedSampleFromSchema,
memoizedCreateXMLExample,
mergeJsonSchema,
} from "core/plugins/json-schema-5-samples/fn/index"
import makeGetSampleSchema from "core/plugins/json-schema-5-samples/fn/get-sample-schema"
import makeGetJsonSampleSchema from "core/plugins/json-schema-5-samples/fn/get-json-sample-schema"
Expand Down Expand Up @@ -103,6 +104,7 @@ describe("bug #4557: default parameter values", function () {
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
mergeJsonSchema,
},
})
const props = {
Expand Down
4 changes: 4 additions & 0 deletions test/unit/components/parameter-row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ParameterRow from "core/components/parameter-row"
import {
memoizedSampleFromSchema,
memoizedCreateXMLExample,
mergeJsonSchema,
} from "core/plugins/json-schema-5-samples/fn/index"
import makeGetSampleSchema from "core/plugins/json-schema-5-samples/fn/get-sample-schema"
import makeGetJsonSampleSchema from "core/plugins/json-schema-5-samples/fn/get-json-sample-schema"
Expand All @@ -31,6 +32,7 @@ describe("<ParameterRow/>", () => {
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
mergeJsonSchema,
},
oas3Selectors: { activeExamplesMember: () => {} },
getConfigs: () => ({}),
Expand Down Expand Up @@ -276,6 +278,7 @@ describe("bug #5573: zero default and example values", function () {
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
mergeJsonSchema,
},
})
const props = {
Expand Down Expand Up @@ -329,6 +332,7 @@ describe("bug #5573: zero default and example values", function () {
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
getSampleSchema: makeGetSampleSchema(getSystem),
mergeJsonSchema,
},
})
const props = {
Expand Down

0 comments on commit 9037acf

Please sign in to comment.