import React from "react"
import PropTypes from "prop-types"
import ImPropTypes from "react-immutable-proptypes"
import cx from "classnames"
import { fromJS, Seq, Iterable, List, Map } from "immutable"
import { getExtensions, fromJSOrdered, stringify } from "core/utils"
import { getKnownSyntaxHighlighterLanguage } from "core/utils/jsonParse"
/* eslint-disable react/jsx-no-bind */
const getExampleComponent = ( sampleResponse, HighlightCode ) => {
if (sampleResponse == null) return null
const testValueForJson = getKnownSyntaxHighlighterLanguage(sampleResponse)
const language = testValueForJson ? "json" : null
return (
{stringify(sampleResponse)}
)
}
export default class Response extends React.Component {
constructor(props, context) {
super(props, context)
this.state = {
responseContentType: "",
}
}
static propTypes = {
path: PropTypes.string.isRequired,
method: PropTypes.string.isRequired,
code: PropTypes.string.isRequired,
response: PropTypes.instanceOf(Iterable),
className: PropTypes.string,
getComponent: PropTypes.func.isRequired,
getConfigs: PropTypes.func.isRequired,
specSelectors: PropTypes.object.isRequired,
oas3Actions: PropTypes.object.isRequired,
specPath: ImPropTypes.list.isRequired,
fn: PropTypes.object.isRequired,
contentType: PropTypes.string,
activeExamplesKey: PropTypes.string,
controlsAcceptHeader: PropTypes.bool,
onContentTypeChange: PropTypes.func
}
static defaultProps = {
response: fromJS({}),
onContentTypeChange: () => {}
}
_onContentTypeChange = (value) => {
const { onContentTypeChange, controlsAcceptHeader } = this.props
this.setState({ responseContentType: value })
onContentTypeChange({
value: value,
controlsAcceptHeader
})
}
getTargetExamplesKey = () => {
const { response, contentType, activeExamplesKey } = this.props
const activeContentType = this.state.responseContentType || contentType
const activeMediaType = response.getIn(["content", activeContentType], Map({}))
const examplesForMediaType = activeMediaType.get("examples", null)
const firstExamplesKey = examplesForMediaType.keySeq().first()
return activeExamplesKey || firstExamplesKey
}
render() {
let {
path,
method,
code,
response,
className,
specPath,
fn,
getComponent,
getConfigs,
specSelectors,
contentType,
controlsAcceptHeader,
oas3Actions,
} = this.props
let { inferSchema, getSampleSchema } = fn
let isOAS3 = specSelectors.isOAS3()
const { showExtensions } = getConfigs()
let extensions = showExtensions ? getExtensions(response) : null
let headers = response.get("headers")
let links = response.get("links")
const ResponseExtension = getComponent("ResponseExtension")
const Headers = getComponent("headers")
const HighlightCode = getComponent("HighlightCode", true)
const ModelExample = getComponent("modelExample")
const Markdown = getComponent("Markdown", true)
const OperationLink = getComponent("operationLink")
const ContentType = getComponent("contentType")
const ExamplesSelect = getComponent("ExamplesSelect")
const Example = getComponent("Example")
var schema, specPathWithPossibleSchema
const activeContentType = this.state.responseContentType || contentType
const activeMediaType = response.getIn(["content", activeContentType], Map({}))
const examplesForMediaType = activeMediaType.get("examples", null)
// Goal: find a schema value for `schema`
if(isOAS3) {
const oas3SchemaForContentType = activeMediaType.get("schema")
schema = oas3SchemaForContentType ? inferSchema(oas3SchemaForContentType.toJS()) : null
specPathWithPossibleSchema = oas3SchemaForContentType ? List(["content", this.state.responseContentType, "schema"]) : specPath
} else {
schema = response.get("schema")
specPathWithPossibleSchema = response.has("schema") ? specPath.push("schema") : specPath
}
let mediaTypeExample
let shouldOverrideSchemaExample = false
let sampleSchema
let sampleGenConfig = {
includeReadOnly: true
}
// Goal: find an example value for `sampleResponse`
if(isOAS3) {
sampleSchema = activeMediaType.get("schema")?.toJS()
if(Map.isMap(examplesForMediaType) && !examplesForMediaType.isEmpty()) {
const targetExamplesKey = this.getTargetExamplesKey()
const targetExample = examplesForMediaType
.get(targetExamplesKey, Map({}))
const getMediaTypeExample = (targetExample) =>
Map.isMap(targetExample)
? targetExample.get("value")
: undefined
mediaTypeExample = getMediaTypeExample(targetExample)
if(mediaTypeExample === undefined) {
mediaTypeExample = getMediaTypeExample(examplesForMediaType.values().next().value)
}
shouldOverrideSchemaExample = true
} else if(activeMediaType.get("example") !== undefined) {
// use the example key's value
mediaTypeExample = activeMediaType.get("example")
shouldOverrideSchemaExample = true
}
} else {
sampleSchema = schema
sampleGenConfig = {...sampleGenConfig, includeWriteOnly: true}
const oldOASMediaTypeExample = response.getIn(["examples", activeContentType])
if(oldOASMediaTypeExample) {
mediaTypeExample = oldOASMediaTypeExample
shouldOverrideSchemaExample = true
}
}
const sampleResponse = getSampleSchema(
sampleSchema,
activeContentType,
sampleGenConfig,
shouldOverrideSchemaExample ? mediaTypeExample : undefined
)
const example = getExampleComponent( sampleResponse, HighlightCode )
return (
{ code }
|
{ !showExtensions || !extensions.size ? null : extensions.entrySeq().map(([key, v]) => )}
{isOAS3 && response.get("content") ? (
Media type
{controlsAcceptHeader ? (
Controls Accept header.
) : null}
{Map.isMap(examplesForMediaType) && !examplesForMediaType.isEmpty() ? (
Examples
oas3Actions.setActiveExamplesMember({
name: key,
pathMethod: [path, method],
contextType: "responses",
contextName: code
})
}
showLabels={false}
/>
) : null}
) : null}
{ example || schema ? (
) : null }
{ isOAS3 && examplesForMediaType ? (
) : null}
{ headers ? (
) : null}
|
{isOAS3 ?
{ links ?
links.toSeq().entrySeq().map(([key, link]) => {
return
})
: No links}
| : null}
)
}
}