fix(components): add support for oneOf/anyOf JSON Schema keywords in parameter-row rendering (#9934)
Refs #7912
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import React, { Component } from "react"
|
import React, { Component } from "react"
|
||||||
import { Map, List } from "immutable"
|
import { Map, List, fromJS } from "immutable"
|
||||||
import PropTypes from "prop-types"
|
import PropTypes from "prop-types"
|
||||||
import ImPropTypes from "react-immutable-proptypes"
|
import ImPropTypes from "react-immutable-proptypes"
|
||||||
import win from "core/window"
|
import win from "core/window"
|
||||||
@@ -97,7 +97,7 @@ export default class ParameterRow extends Component {
|
|||||||
let { specSelectors, pathMethod, rawParam, oas3Selectors, fn } = this.props
|
let { specSelectors, pathMethod, rawParam, oas3Selectors, fn } = this.props
|
||||||
|
|
||||||
const paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
|
const paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
|
||||||
const { schema } = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
|
let { schema } = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
|
||||||
const parameterMediaType = paramWithMeta
|
const parameterMediaType = paramWithMeta
|
||||||
.get("content", Map())
|
.get("content", Map())
|
||||||
.keySeq()
|
.keySeq()
|
||||||
@@ -126,6 +126,8 @@ export default class ParameterRow extends Component {
|
|||||||
? paramWithMeta.getIn(["schema", "example"])
|
? paramWithMeta.getIn(["schema", "example"])
|
||||||
: (schema && schema.getIn(["default"]))
|
: (schema && schema.getIn(["default"]))
|
||||||
} else if (specSelectors.isOAS3()) {
|
} else if (specSelectors.isOAS3()) {
|
||||||
|
schema = this.composeJsonSchema(schema)
|
||||||
|
|
||||||
const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey())
|
const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey())
|
||||||
initialValue =
|
initialValue =
|
||||||
paramWithMeta.getIn(["examples", currentExampleKey, "value"]) !== undefined
|
paramWithMeta.getIn(["examples", currentExampleKey, "value"]) !== undefined
|
||||||
@@ -181,6 +183,13 @@ export default class ParameterRow extends Component {
|
|||||||
return `${param.get("name")}-${param.get("in")}`
|
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() {
|
render() {
|
||||||
let {param, rawParam, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath, oas3Selectors} = this.props
|
let {param, rawParam, getComponent, getConfigs, isExecute, fn, onChangeConsumes, specSelectors, pathMethod, specPath, oas3Selectors} = this.props
|
||||||
|
|
||||||
@@ -222,6 +231,10 @@ export default class ParameterRow extends Component {
|
|||||||
let { schema } = getParameterSchema(param, { isOAS3 })
|
let { schema } = getParameterSchema(param, { isOAS3 })
|
||||||
let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
|
let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
|
||||||
|
|
||||||
|
if (isOAS3) {
|
||||||
|
schema = this.composeJsonSchema(schema)
|
||||||
|
}
|
||||||
|
|
||||||
let format = schema ? schema.get("format") : null
|
let format = schema ? schema.get("format") : null
|
||||||
let type = schema ? schema.get("type") : null
|
let type = schema ? schema.get("type") : null
|
||||||
let itemType = schema ? schema.getIn(["items", "type"]) : null
|
let itemType = schema ? schema.getIn(["items", "type"]) : null
|
||||||
|
|||||||
49
test/e2e-cypress/e2e/features/parameters-one-of-any-of.cy.js
Normal file
49
test/e2e-cypress/e2e/features/parameters-one-of-any-of.cy.js
Normal file
@@ -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\"")
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -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
|
||||||
@@ -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
|
||||||
@@ -9,6 +9,7 @@ import ParameterRow from "core/components/parameter-row"
|
|||||||
import {
|
import {
|
||||||
memoizedSampleFromSchema,
|
memoizedSampleFromSchema,
|
||||||
memoizedCreateXMLExample,
|
memoizedCreateXMLExample,
|
||||||
|
mergeJsonSchema,
|
||||||
} from "core/plugins/json-schema-5-samples/fn/index"
|
} from "core/plugins/json-schema-5-samples/fn/index"
|
||||||
import makeGetSampleSchema from "core/plugins/json-schema-5-samples/fn/get-sample-schema"
|
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"
|
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 () {
|
|||||||
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
|
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
|
||||||
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
|
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
|
||||||
getSampleSchema: makeGetSampleSchema(getSystem),
|
getSampleSchema: makeGetSampleSchema(getSystem),
|
||||||
|
mergeJsonSchema,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const props = {
|
const props = {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import ParameterRow from "core/components/parameter-row"
|
|||||||
import {
|
import {
|
||||||
memoizedSampleFromSchema,
|
memoizedSampleFromSchema,
|
||||||
memoizedCreateXMLExample,
|
memoizedCreateXMLExample,
|
||||||
|
mergeJsonSchema,
|
||||||
} from "core/plugins/json-schema-5-samples/fn/index"
|
} from "core/plugins/json-schema-5-samples/fn/index"
|
||||||
import makeGetSampleSchema from "core/plugins/json-schema-5-samples/fn/get-sample-schema"
|
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"
|
import makeGetJsonSampleSchema from "core/plugins/json-schema-5-samples/fn/get-json-sample-schema"
|
||||||
@@ -31,6 +32,7 @@ describe("<ParameterRow/>", () => {
|
|||||||
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
|
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
|
||||||
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
|
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
|
||||||
getSampleSchema: makeGetSampleSchema(getSystem),
|
getSampleSchema: makeGetSampleSchema(getSystem),
|
||||||
|
mergeJsonSchema,
|
||||||
},
|
},
|
||||||
oas3Selectors: { activeExamplesMember: () => {} },
|
oas3Selectors: { activeExamplesMember: () => {} },
|
||||||
getConfigs: () => ({}),
|
getConfigs: () => ({}),
|
||||||
@@ -276,6 +278,7 @@ describe("bug #5573: zero default and example values", function () {
|
|||||||
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
|
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
|
||||||
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
|
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
|
||||||
getSampleSchema: makeGetSampleSchema(getSystem),
|
getSampleSchema: makeGetSampleSchema(getSystem),
|
||||||
|
mergeJsonSchema,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const props = {
|
const props = {
|
||||||
@@ -329,6 +332,7 @@ describe("bug #5573: zero default and example values", function () {
|
|||||||
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
|
getYamlSampleSchema: makeGetYamlSampleSchema(getSystem),
|
||||||
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
|
getXmlSampleSchema: makeGetXmlSampleSchema(getSystem),
|
||||||
getSampleSchema: makeGetSampleSchema(getSystem),
|
getSampleSchema: makeGetSampleSchema(getSystem),
|
||||||
|
mergeJsonSchema,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
const props = {
|
const props = {
|
||||||
|
|||||||
Reference in New Issue
Block a user