import React from "react"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import { Map, OrderedMap, List } from "immutable"
import { getCommonExtensions, getSampleSchema, stringify, isEmptyValue } from "core/utils"
export const getDefaultRequestBodyValue = (requestBody, mediaType, activeExamplesKey) => {
const mediaTypeValue = requestBody.getIn(["content", mediaType])
const schema = mediaTypeValue.get("schema").toJS()
const hasExamplesKey = mediaTypeValue.get("examples") !== undefined
const exampleSchema = mediaTypeValue.get("example")
const mediaTypeExample = hasExamplesKey
? mediaTypeValue.getIn([
"examples",
activeExamplesKey,
"value"
])
: exampleSchema
const exampleValue = getSampleSchema(
schema,
mediaType,
{
includeWriteOnly: true
},
mediaTypeExample
)
return stringify(exampleValue)
}
const RequestBody = ({
userHasEditedBody,
requestBody,
requestBodyValue,
requestBodyInclusionSetting,
requestBodyErrors,
getComponent,
getConfigs,
specSelectors,
fn,
contentType,
isExecute,
specPath,
onChange,
onChangeIncludeEmpty,
activeExamplesKey,
updateActiveExamplesKey,
setRetainRequestBodyValueFlag
}) => {
const handleFile = (e) => {
onChange(e.target.files[0])
}
const setIsIncludedOptions = (key) => {
let options = {
key,
shouldDispatchInit: false,
defaultValue: true
}
let currentInclusion = requestBodyInclusionSetting.get(key, "no value")
if (currentInclusion === "no value") {
options.shouldDispatchInit = true
// future: can get/set defaultValue from a config setting
}
return options
}
const Markdown = getComponent("Markdown", true)
const ModelExample = getComponent("modelExample")
const RequestBodyEditor = getComponent("RequestBodyEditor")
const HighlightCode = getComponent("highlightCode")
const ExamplesSelectValueRetainer = getComponent("ExamplesSelectValueRetainer")
const Example = getComponent("Example")
const ParameterIncludeEmpty = getComponent("ParameterIncludeEmpty")
const { showCommonExtensions } = getConfigs()
const requestBodyDescription = (requestBody && requestBody.get("description")) || null
const requestBodyContent = (requestBody && requestBody.get("content")) || new OrderedMap()
contentType = contentType || requestBodyContent.keySeq().first() || ""
const mediaTypeValue = requestBodyContent.get(contentType, OrderedMap())
const schemaForMediaType = mediaTypeValue.get("schema", OrderedMap())
const examplesForMediaType = mediaTypeValue.get("examples", null)
const handleExamplesSelect = (key /*, { isSyntheticChange } */) => {
updateActiveExamplesKey(key)
}
requestBodyErrors = List.isList(requestBodyErrors) ? requestBodyErrors : List()
if(!mediaTypeValue.size) {
return null
}
const isObjectContent = mediaTypeValue.getIn(["schema", "type"]) === "object"
if(
contentType === "application/octet-stream"
|| contentType.indexOf("image/") === 0
|| contentType.indexOf("audio/") === 0
|| contentType.indexOf("video/") === 0
) {
const Input = getComponent("Input")
if(!isExecute) {
return
Example values are not available for application/octet-stream media types.
}
return
}
if (
isObjectContent &&
(
contentType === "application/x-www-form-urlencoded" ||
contentType.indexOf("multipart/") === 0
) &&
schemaForMediaType.get("properties", OrderedMap()).size > 0
) {
const JsonSchemaForm = getComponent("JsonSchemaForm")
const ParameterExt = getComponent("ParameterExt")
const bodyProperties = schemaForMediaType.get("properties", OrderedMap())
requestBodyValue = Map.isMap(requestBodyValue) ? requestBodyValue : OrderedMap()
return
|
{ key }
{ !required ? null : * }
{ type }
{ format && (${format})}
{!showCommonExtensions || !commonExt.size ? null : commonExt.entrySeq().map(([key, v]) =>
{ prop.get("deprecated") ? "deprecated": null }
|
|