Skip to content

Commit 9037acf

Browse files
authored
fix(components): add support for oneOf/anyOf JSON Schema keywords in parameter-row rendering (#9934)
Refs #7912
1 parent dcc87aa commit 9037acf

File tree

6 files changed

+170
-2
lines changed

6 files changed

+170
-2
lines changed

src/core/components/parameter-row.jsx

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { Component } from "react"
2-
import { Map, List } from "immutable"
2+
import { Map, List, fromJS } from "immutable"
33
import PropTypes from "prop-types"
44
import ImPropTypes from "react-immutable-proptypes"
55
import win from "core/window"
@@ -97,7 +97,7 @@ export default class ParameterRow extends Component {
9797
let { specSelectors, pathMethod, rawParam, oas3Selectors, fn } = this.props
9898

9999
const paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
100-
const { schema } = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
100+
let { schema } = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
101101
const parameterMediaType = paramWithMeta
102102
.get("content", Map())
103103
.keySeq()
@@ -126,6 +126,8 @@ export default class ParameterRow extends Component {
126126
? paramWithMeta.getIn(["schema", "example"])
127127
: (schema && schema.getIn(["default"]))
128128
} else if (specSelectors.isOAS3()) {
129+
schema = this.composeJsonSchema(schema)
130+
129131
const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey())
130132
initialValue =
131133
paramWithMeta.getIn(["examples", currentExampleKey, "value"]) !== undefined
@@ -181,6 +183,13 @@ export default class ParameterRow extends Component {
181183
return `${param.get("name")}-${param.get("in")}`
182184
}
183185

186+
composeJsonSchema(schema) {
187+
const { fn } = this.props
188+
const oneOf = schema.get("oneOf")?.get(0)?.toJS()
189+
const anyOf = schema.get("anyOf")?.get(0)?.toJS()
190+
return fromJS(fn.mergeJsonSchema(schema.toJS(), oneOf ?? anyOf ?? {}))
191+
}
192+
184193
render() {
185194
let {param, rawParam, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath, oas3Selectors} = this.props
186195

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

234+
if (isOAS3) {
235+
schema = this.composeJsonSchema(schema)
236+
}
237+
225238
let format = schema ? schema.get("format") : null
226239
let type = schema ? schema.get("type") : null
227240
let itemType = schema ? schema.getIn(["items", "type"]) : null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @prettier
3+
*/
4+
5+
describe("Parameter with oneOf and anyOf keywords in OpenAPI 3.0.x", () => {
6+
it("should render correct form fields", () => {
7+
cy.visit("/?url=/documents/features/parameters-one-of-any-of-oas3.yaml")
8+
.get("#operations-default-get_")
9+
.click()
10+
cy.get(".parameters-col_description")
11+
.eq(1)
12+
.find("select")
13+
.should("exist")
14+
.and("have.value", "ascending")
15+
cy.get(".parameters-col_description")
16+
.eq(2)
17+
.find("input")
18+
.should("exist")
19+
.and("have.value", "test")
20+
cy.get(".parameters-col_description")
21+
.eq(3)
22+
.find("textarea")
23+
.should("exist")
24+
.and("contain", "\"eq\": \"active\"")
25+
})
26+
})
27+
28+
describe("Parameter with oneOf and anyOf keywords in OpenAPI 3.1.0.", () => {
29+
it("should render correct form fields", () => {
30+
cy.visit("/?url=/documents/features/parameters-one-of-any-of-oas31.yaml")
31+
.get("#operations-default-get_")
32+
.click()
33+
cy.get(".parameters-col_description")
34+
.eq(1)
35+
.find("select")
36+
.should("exist")
37+
.and("have.value", "ascending")
38+
cy.get(".parameters-col_description")
39+
.eq(2)
40+
.find("input")
41+
.should("exist")
42+
.and("have.value", "test")
43+
cy.get(".parameters-col_description")
44+
.eq(3)
45+
.find("textarea")
46+
.should("exist")
47+
.and("contain", "\"eq\": \"active\"")
48+
})
49+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
openapi: 3.0.0
2+
paths:
3+
/:
4+
get:
5+
parameters:
6+
- name: enum
7+
in: query
8+
schema:
9+
oneOf:
10+
- type: string
11+
default: ascending
12+
enum:
13+
- ascending
14+
- descending
15+
- name: string
16+
in: query
17+
default: test
18+
schema:
19+
anyOf:
20+
- type: string
21+
- name: object
22+
in: query
23+
schema:
24+
oneOf:
25+
- type: object
26+
properties:
27+
eq:
28+
type: string
29+
enum:
30+
- active
31+
- archived
32+
neq:
33+
type: string
34+
enum:
35+
- active
36+
- archived
37+
in:
38+
type: array
39+
items:
40+
type: string
41+
enum:
42+
- active
43+
- archived
44+
notIn:
45+
type: array
46+
items:
47+
type: string
48+
enum:
49+
- active
50+
- archived
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
openapi: 3.1.0
2+
paths:
3+
/:
4+
get:
5+
parameters:
6+
- name: enum
7+
in: query
8+
schema:
9+
oneOf:
10+
- type: string
11+
default: ascending
12+
enum:
13+
- ascending
14+
- descending
15+
- name: string
16+
in: query
17+
default: test
18+
schema:
19+
anyOf:
20+
- type: string
21+
- name: object
22+
in: query
23+
schema:
24+
oneOf:
25+
- type: object
26+
properties:
27+
eq:
28+
type: string
29+
enum:
30+
- active
31+
- archived
32+
neq:
33+
type: string
34+
enum:
35+
- active
36+
- archived
37+
in:
38+
type: array
39+
items:
40+
type: string
41+
enum:
42+
- active
43+
- archived
44+
notIn:
45+
type: array
46+
items:
47+
type: string
48+
enum:
49+
- active
50+
- archived

test/unit/bugs/4557-default-parameter-values.jsx

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ParameterRow from "core/components/parameter-row"
99
import {
1010
memoizedSampleFromSchema,
1111
memoizedCreateXMLExample,
12+
mergeJsonSchema,
1213
} from "core/plugins/json-schema-5-samples/fn/index"
1314
import makeGetSampleSchema from "core/plugins/json-schema-5-samples/fn/get-sample-schema"
1415
import makeGetJsonSampleSchema from "core/plugins/json-schema-5-samples/fn/get-json-sample-schema"
@@ -103,6 +104,7 @@ describe("bug #4557: default parameter values", function () {
103104
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
104105
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
105106
getSampleSchema: makeGetSampleSchema(getSystem),
107+
mergeJsonSchema,
106108
},
107109
})
108110
const props = {

test/unit/components/parameter-row.jsx

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ParameterRow from "core/components/parameter-row"
99
import {
1010
memoizedSampleFromSchema,
1111
memoizedCreateXMLExample,
12+
mergeJsonSchema,
1213
} from "core/plugins/json-schema-5-samples/fn/index"
1314
import makeGetSampleSchema from "core/plugins/json-schema-5-samples/fn/get-sample-schema"
1415
import makeGetJsonSampleSchema from "core/plugins/json-schema-5-samples/fn/get-json-sample-schema"
@@ -31,6 +32,7 @@ describe("<ParameterRow/>", () => {
3132
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
3233
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
3334
getSampleSchema: makeGetSampleSchema(getSystem),
35+
mergeJsonSchema,
3436
},
3537
oas3Selectors: { activeExamplesMember: () => {} },
3638
getConfigs: () => ({}),
@@ -276,6 +278,7 @@ describe("bug #5573: zero default and example values", function () {
276278
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
277279
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
278280
getSampleSchema: makeGetSampleSchema(getSystem),
281+
mergeJsonSchema,
279282
},
280283
})
281284
const props = {
@@ -329,6 +332,7 @@ describe("bug #5573: zero default and example values", function () {
329332
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
330333
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
331334
getSampleSchema: makeGetSampleSchema(getSystem),
335+
mergeJsonSchema,
332336
},
333337
})
334338
const props = {

0 commit comments

Comments
 (0)