feat: sample-gen mulit and form media-type (#6874)

* application/x-www-form-urlencoded || **multipart/** initial value should rely on sample gen
This commit is contained in:
Mahtis Michel
2021-02-03 21:04:12 +01:00
committed by GitHub
parent 23323bbbff
commit 8ed6c34958
2 changed files with 61 additions and 59 deletions

View File

@@ -129,8 +129,9 @@ export class JsonSchema_array extends PureComponent {
} }
componentWillReceiveProps(props) { componentWillReceiveProps(props) {
if(props.value !== this.state.value) const value = valueOrEmptyList(props.value)
this.setState({ value: props.value }) if(value !== this.state.value)
this.setState({ value })
if(props.schema !== this.state.schema) if(props.schema !== this.state.schema)
this.setState({ schema: props.schema }) this.setState({ schema: props.schema })
@@ -151,7 +152,7 @@ export class JsonSchema_array extends PureComponent {
value: value.delete(i) value: value.delete(i)
}), this.onChange) }), this.onChange)
} }
addItem = () => { addItem = () => {
let newValue = valueOrEmptyList(this.state.value) let newValue = valueOrEmptyList(this.state.value)
this.setState(() => ({ this.setState(() => ({
@@ -382,5 +383,5 @@ export class JsonSchema_object extends PureComponent {
} }
function valueOrEmptyList(value) { function valueOrEmptyList(value) {
return List.isList(value) ? value : List() return List.isList(value) ? value : Array.isArray(value) ? fromJS(value) : List()
} }

View File

@@ -133,7 +133,7 @@ const RequestBody = ({
<table> <table>
<tbody> <tbody>
{ {
Map.isMap(bodyProperties) && bodyProperties.entrySeq().map(([key, prop]) => { Map.isMap(bodyProperties) && bodyProperties.entrySeq().map(([key, prop]) => {
if (prop.get("readOnly")) return if (prop.get("readOnly")) return
let commonExt = showCommonExtensions ? getCommonExtensions(prop) : null let commonExt = showCommonExtensions ? getCommonExtensions(prop) : null
@@ -143,67 +143,68 @@ const RequestBody = ({
const description = prop.get("description") const description = prop.get("description")
const currentValue = requestBodyValue.getIn([key, "value"]) const currentValue = requestBodyValue.getIn([key, "value"])
const currentErrors = requestBodyValue.getIn([key, "errors"]) || requestBodyErrors const currentErrors = requestBodyValue.getIn([key, "errors"]) || requestBodyErrors
const included = requestBodyInclusionSetting.get(key) || false
let initialValue = prop.get("default") || prop.get("example") || "" let hasNonEmptyInitialVal = prop.has("default") || prop.has("example") || prop.hasIn(["items", "example"]) || prop.hasIn(["items", "default"]) || prop.has("enum") && prop.get("enum").size === 1
let initialValue = ""
if (initialValue === "") { if(type === "array" && !hasNonEmptyInitialVal) {
if(type === "object") { initialValue = []
initialValue = getSampleSchema(prop, false, { } else if (hasNonEmptyInitialVal) {
includeWriteOnly: true // TODO: what about example or examples from requestBody could be passed as exampleOverride
}) initialValue = getSampleSchema(prop, false, {
} else if(type === "array") { includeWriteOnly: true
initialValue = [] })
}
} }
if (typeof initialValue !== "string" && type === "object") { if (typeof initialValue !== "string" && type === "object") {
initialValue = stringify(initialValue) initialValue = stringify(initialValue)
}
if (typeof initialValue === "string" && type === "array") {
initialValue = JSON.parse(initialValue)
} }
const isFile = type === "string" && (format === "binary" || format === "base64") const isFile = type === "string" && (format === "binary" || format === "base64")
return <tr key={key} className="parameters" data-property-name={key}> return <tr key={key} className="parameters" data-property-name={key}>
<td className="parameters-col_name"> <td className="parameters-col_name">
<div className={required ? "parameter__name required" : "parameter__name"}> <div className={required ? "parameter__name required" : "parameter__name"}>
{ key } { key }
{ !required ? null : <span>&nbsp;*</span> } { !required ? null : <span>&nbsp;*</span> }
</div> </div>
<div className="parameter__type"> <div className="parameter__type">
{ type } { type }
{ format && <span className="prop-format">(${format})</span>} { format && <span className="prop-format">(${format})</span>}
{!showCommonExtensions || !commonExt.size ? null : commonExt.entrySeq().map(([key, v]) => <ParameterExt key={`${key}-${v}`} xKey={key} xVal={v} />)} {!showCommonExtensions || !commonExt.size ? null : commonExt.entrySeq().map(([key, v]) => <ParameterExt key={`${key}-${v}`} xKey={key} xVal={v} />)}
</div> </div>
<div className="parameter__deprecated"> <div className="parameter__deprecated">
{ prop.get("deprecated") ? "deprecated": null } { prop.get("deprecated") ? "deprecated": null }
</div> </div>
</td> </td>
<td className="parameters-col_description"> <td className="parameters-col_description">
<Markdown source={ description }></Markdown> <Markdown source={ description }></Markdown>
{isExecute ? <div> {isExecute ? <div>
<JsonSchemaForm <JsonSchemaForm
fn={fn} fn={fn}
dispatchInitialValue={!isFile} dispatchInitialValue={!isFile}
schema={prop} schema={prop}
description={key} description={key}
getComponent={getComponent} getComponent={getComponent}
value={currentValue === undefined ? initialValue : currentValue} value={currentValue === undefined ? initialValue : currentValue}
required = { required } required = { required }
errors = { currentErrors } errors = { currentErrors }
onChange={(value) => { onChange={(value) => {
onChange(value, [key]) onChange(value, [key])
}} }}
/> />
{required ? null : ( {required ? null : (
<ParameterIncludeEmpty <ParameterIncludeEmpty
onChange={(value) => onChangeIncludeEmpty(key, value)} onChange={(value) => onChangeIncludeEmpty(key, value)}
isIncluded={requestBodyInclusionSetting.get(key) || false} isIncluded={included}
isIncludedOptions={setIsIncludedOptions(key)} isIncludedOptions={setIsIncludedOptions(key)}
isDisabled={Array.isArray(currentValue) ? currentValue.length !== 0 : !isEmptyValue(currentValue)} isDisabled={Array.isArray(currentValue) ? currentValue.length !== 0 : !isEmptyValue(currentValue)}
/> />
)} )}
</div> : null } </div> : null }
</td> </td>
</tr> </tr>
}) })
} }
</tbody> </tbody>