feature: support for Parameter.content (#5571)
* add `getParameterSchema` OAS helper * use `Parameter.content.[firstKey].schema` as schema value when present * `newValue` -> `initialValue` * make `paramWithMeta` a const * add trailing comma to `swagger2SchemaKeys` * refactor `helpers` to a folder * deprecate `src/core/utils.js` in favor of `src/core/helpers/` * support `Parameter.content.[mediaType].schema` in validateParam * reject `null` as an OAS3 object value * expose Fetch errors in the browser console * generate ParameterRow default values based on `content` values * add tests for `getParameterSchema` * remove debugger statement * remove debugger statement * don't apply `generatedSampleValue`s to parameters with `examples` * remove extra semi * disable JSON check in parameter runtime validation * stringify JsonSchema_object textarea values * add Cypress tests * swagger-client@3.9.4
This commit is contained in:
@@ -3,7 +3,8 @@ import { Map, List } from "immutable"
|
||||
import PropTypes from "prop-types"
|
||||
import ImPropTypes from "react-immutable-proptypes"
|
||||
import win from "core/window"
|
||||
import { getExtensions, getCommonExtensions, numberToString, stringify } from "core/utils"
|
||||
import { getSampleSchema, getExtensions, getCommonExtensions, numberToString, stringify } from "core/utils"
|
||||
import getParameterSchema from "../../helpers/get-parameter-schema.js"
|
||||
|
||||
export default class ParameterRow extends Component {
|
||||
static propTypes = {
|
||||
@@ -40,7 +41,7 @@ export default class ParameterRow extends Component {
|
||||
let enumValue
|
||||
|
||||
if(isOAS3) {
|
||||
let schema = parameterWithMeta.get("schema") || Map()
|
||||
let schema = getParameterSchema(parameterWithMeta, { isOAS3 })
|
||||
enumValue = schema.get("enum")
|
||||
} else {
|
||||
enumValue = parameterWithMeta ? parameterWithMeta.get("enum") : undefined
|
||||
@@ -95,30 +96,68 @@ export default class ParameterRow extends Component {
|
||||
setDefaultValue = () => {
|
||||
let { specSelectors, pathMethod, rawParam, oas3Selectors } = this.props
|
||||
|
||||
let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
|
||||
const paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
|
||||
|
||||
const schema = getParameterSchema(paramWithMeta, { isOAS3: specSelectors.isOAS3() })
|
||||
|
||||
const parameterMediaType = paramWithMeta
|
||||
.get("content", Map())
|
||||
.keySeq()
|
||||
.first()
|
||||
|
||||
const generatedSampleValue = getSampleSchema(schema.toJS(), parameterMediaType, {
|
||||
includeWriteOnly: true
|
||||
})
|
||||
|
||||
if (!paramWithMeta || paramWithMeta.get("value") !== undefined) {
|
||||
return
|
||||
}
|
||||
|
||||
if( paramWithMeta.get("in") !== "body" ) {
|
||||
let newValue
|
||||
let initialValue
|
||||
|
||||
//// Find an initial value
|
||||
|
||||
if (specSelectors.isSwagger2()) {
|
||||
newValue = paramWithMeta.get("x-example")
|
||||
|| paramWithMeta.getIn(["default"])
|
||||
initialValue = paramWithMeta.get("x-example")
|
||||
|| paramWithMeta.getIn(["schema", "example"])
|
||||
|| paramWithMeta.getIn(["schema", "default"])
|
||||
|| schema.getIn(["default"])
|
||||
} else if (specSelectors.isOAS3()) {
|
||||
const currentExampleKey = oas3Selectors.activeExamplesMember(...pathMethod, "parameters", this.getParamKey())
|
||||
newValue = paramWithMeta.getIn(["examples", currentExampleKey, "value"])
|
||||
initialValue = paramWithMeta.getIn(["examples", currentExampleKey, "value"])
|
||||
|| paramWithMeta.getIn(["content", parameterMediaType, "example"])
|
||||
|| paramWithMeta.get("example")
|
||||
|| paramWithMeta.getIn(["schema", "example"])
|
||||
|| paramWithMeta.getIn(["schema", "default"])
|
||||
|| schema.get("example")
|
||||
|| schema.get("default")
|
||||
}
|
||||
if(newValue !== undefined) {
|
||||
|
||||
//// Process the initial value
|
||||
|
||||
if(initialValue !== undefined && !List.isList(initialValue)) {
|
||||
// Stringify if it isn't a List
|
||||
initialValue = stringify(initialValue)
|
||||
}
|
||||
|
||||
//// Dispatch the initial value
|
||||
|
||||
if(initialValue !== undefined) {
|
||||
this.onChangeWrapper(initialValue)
|
||||
} else if(
|
||||
schema.get("type") === "object"
|
||||
&& generatedSampleValue
|
||||
&& !paramWithMeta.get("examples")
|
||||
) {
|
||||
// Object parameters get special treatment.. if the user doesn't set any
|
||||
// default or example values, we'll provide initial values generated from
|
||||
// the schema.
|
||||
// However, if `examples` exist for the parameter, we won't do anything,
|
||||
// so that the appropriate `examples` logic can take over.
|
||||
this.onChangeWrapper(
|
||||
List.isList(newValue) ? newValue : stringify(newValue)
|
||||
List.isList(generatedSampleValue) ? (
|
||||
generatedSampleValue
|
||||
) : (
|
||||
stringify(generatedSampleValue)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -171,7 +210,7 @@ export default class ParameterRow extends Component {
|
||||
|
||||
let paramWithMeta = specSelectors.parameterWithMetaByIdentity(pathMethod, rawParam) || Map()
|
||||
let format = param.get("format")
|
||||
let schema = isOAS3 ? param.get("schema") : param
|
||||
let schema = getParameterSchema(param, { isOAS3 })
|
||||
let type = schema.get("type")
|
||||
let isFormData = inType === "formData"
|
||||
let isFormDataSupported = "FormData" in win
|
||||
@@ -285,7 +324,7 @@ export default class ParameterRow extends Component {
|
||||
getConfigs={ getConfigs }
|
||||
isExecute={ isExecute }
|
||||
specSelectors={ specSelectors }
|
||||
schema={ param.get("schema") }
|
||||
schema={ schema }
|
||||
example={ bodyParam }/>
|
||||
: null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user