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

Refs #7912
  • Loading branch information
glowcloud committed May 13, 2024
1 parent 94f2d82 commit de634aa
Show file tree
Hide file tree
Showing 5 changed files with 97 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.mergeJsonSchema(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")}`
}

mergeJsonSchema(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.mergeJsonSchema(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
26 changes: 26 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,26 @@
/**
* @prettier
*/

describe("Parameter with oneOf and anyOf keywords", () => {
it("should render correct form fields", () => {
cy.visit("/?url=/documents/features/parameters-one-of-any-of.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
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 de634aa

Please sign in to comment.