fix(validateParam): validate JSON parameter values + support Parameter.content (#5657)

* improve(getParameterSchema): ParameterSchemaDescriptor pattern

* chore: update usage of `getParameterSchema`

* consider `Parameter.content` media type when validating JSON values
This commit is contained in:
kyle
2019-10-11 11:20:23 -07:00
committed by GitHub
parent 71a17f801e
commit 75a0e5d5dc
5 changed files with 190 additions and 51 deletions

View File

@@ -23,6 +23,12 @@ const swagger2SchemaKeys = Im.Set.of(
"multipleOf"
)
/**
* @typedef {Object} ParameterSchemaDescriptor
* @property {Immutable.Map} schema - the parameter schema
* @property {string|null} parameterContentMediaType - the effective media type, for `content`-based OpenAPI 3.0 Parameters, or `null` otherwise
*/
/**
* Get the effective schema value for a parameter, or an empty Immutable.Map if
* no suitable schema can be found.
@@ -35,18 +41,29 @@ const swagger2SchemaKeys = Im.Set.of(
* @param {object} config
* @param {boolean} config.isOAS3 Whether the parameter is from an OpenAPI 2.0
* or OpenAPI 3.0 definition
* @return {Immutable.Map} The desired schema
* @return {ParameterSchemaDescriptor} Information about the parameter schema
*/
export default function getParameterSchema(parameter, { isOAS3 } = {}) {
// Return empty Map if `parameter` isn't a Map
if (!Im.Map.isMap(parameter)) return Im.Map()
if (!Im.Map.isMap(parameter)) {
return {
schema: Im.Map(),
parameterContentMediaType: null,
}
}
if (!isOAS3) {
// Swagger 2.0
if (parameter.get("in") === "body") {
return parameter.get("schema", Im.Map())
return {
schema: parameter.get("schema", Im.Map()),
parameterContentMediaType: null,
}
} else {
return parameter.filter((v, k) => swagger2SchemaKeys.includes(k))
return {
schema: parameter.filter((v, k) => swagger2SchemaKeys.includes(k)),
parameterContentMediaType: null,
}
}
}
@@ -57,11 +74,19 @@ export default function getParameterSchema(parameter, { isOAS3 } = {}) {
.get("content", Im.Map({}))
.keySeq()
return parameter.getIn(
["content", parameterContentMediaTypes.first(), "schema"],
Im.Map()
)
const parameterContentMediaType = parameterContentMediaTypes.first()
return {
schema: parameter.getIn(
["content", parameterContentMediaType, "schema"],
Im.Map()
),
parameterContentMediaType,
}
}
return parameter.get("schema", Im.Map())
return {
schema: parameter.get("schema", Im.Map()),
parameterContentMediaType: null,
}
}