fix(oas3): fix getting initial values for request body in OpenAPI 3.x (#9762)
Refs #9745
This commit is contained in:
@@ -150,39 +150,53 @@ const RequestBody = ({
|
||||
<table>
|
||||
<tbody>
|
||||
{
|
||||
Map.isMap(bodyProperties) && bodyProperties.entrySeq().map(([key, prop]) => {
|
||||
if (prop.get("readOnly")) return
|
||||
Map.isMap(bodyProperties) && bodyProperties.entrySeq().map(([key, schema]) => {
|
||||
if (schema.get("readOnly")) return
|
||||
|
||||
const schemaWithoutKeywords = schema.filter((v, k) => k !== "oneOf"
|
||||
&& k !== "anyOf"
|
||||
&& k !== "$$ref"
|
||||
)
|
||||
|
||||
let commonExt = showCommonExtensions ? getCommonExtensions(prop) : null
|
||||
if (schemaWithoutKeywords.size === 0) {
|
||||
const oneOf = schema.get("oneOf")
|
||||
const anyOf = schema.get("anyOf")
|
||||
const nestedSchema = List.isList(oneOf)
|
||||
? oneOf.get(0)
|
||||
: List.isList(anyOf)
|
||||
? anyOf.get(0)
|
||||
: null
|
||||
|
||||
if (Map.isMap(nestedSchema)) {
|
||||
schema = nestedSchema
|
||||
}
|
||||
}
|
||||
|
||||
let commonExt = showCommonExtensions ? getCommonExtensions(schema) : null
|
||||
const required = schemaForMediaType.get("required", List()).includes(key)
|
||||
const type = prop.get("type")
|
||||
const format = prop.get("format")
|
||||
const description = prop.get("description")
|
||||
const type = schema.get("type")
|
||||
const format = schema.get("format")
|
||||
const description = schema.get("description")
|
||||
const currentValue = requestBodyValue.getIn([key, "value"])
|
||||
const currentErrors = requestBodyValue.getIn([key, "errors"]) || requestBodyErrors
|
||||
const included = requestBodyInclusionSetting.get(key) || false
|
||||
|
||||
const useInitialValFromSchemaSamples = prop.has("default")
|
||||
|| prop.has("example")
|
||||
|| prop.hasIn(["items", "example"])
|
||||
|| prop.hasIn(["items", "default"])
|
||||
const useInitialValFromEnum = prop.has("enum") && (prop.get("enum").size === 1 || required)
|
||||
const useInitialValue = useInitialValFromSchemaSamples || useInitialValFromEnum
|
||||
|
||||
let initialValue = ""
|
||||
if (type === "array" && !useInitialValue) {
|
||||
initialValue = []
|
||||
let initialValue = fn.getSampleSchema(schema, false, {
|
||||
includeWriteOnly: true
|
||||
})
|
||||
|
||||
if (initialValue === false) {
|
||||
initialValue = "false"
|
||||
}
|
||||
if (type === "object" || useInitialValue) {
|
||||
// TODO: what about example or examples from requestBody could be passed as exampleOverride
|
||||
initialValue = fn.getSampleSchema(prop, false, {
|
||||
includeWriteOnly: true
|
||||
})
|
||||
|
||||
if (initialValue === 0) {
|
||||
initialValue = "0"
|
||||
}
|
||||
|
||||
if (typeof initialValue !== "string" && type === "object") {
|
||||
initialValue = stringify(initialValue)
|
||||
}
|
||||
|
||||
if (typeof initialValue === "string" && type === "array") {
|
||||
initialValue = JSON.parse(initialValue)
|
||||
}
|
||||
@@ -201,7 +215,7 @@ const RequestBody = ({
|
||||
{!showCommonExtensions || !commonExt.size ? null : commonExt.entrySeq().map(([key, v]) => <ParameterExt key={`${key}-${v}`} xKey={key} xVal={v} />)}
|
||||
</div>
|
||||
<div className="parameter__deprecated">
|
||||
{ prop.get("deprecated") ? "deprecated": null }
|
||||
{ schema.get("deprecated") ? "deprecated": null }
|
||||
</div>
|
||||
</td>
|
||||
<td className="parameters-col_description">
|
||||
@@ -210,7 +224,7 @@ const RequestBody = ({
|
||||
<JsonSchemaForm
|
||||
fn={fn}
|
||||
dispatchInitialValue={!isFile}
|
||||
schema={prop}
|
||||
schema={schema}
|
||||
description={key}
|
||||
getComponent={getComponent}
|
||||
value={currentValue === undefined ? initialValue : currentValue}
|
||||
|
||||
Reference in New Issue
Block a user