Request Body examples should respect media-type (#6739)
* fix: request body.jsx should rely on the getSampleSchema utility * test: add test to ensure xml request body
This commit is contained in:
@@ -5,31 +5,28 @@ import { Map, OrderedMap, List } from "immutable"
|
|||||||
import { getCommonExtensions, getSampleSchema, stringify, isEmptyValue } from "core/utils"
|
import { getCommonExtensions, getSampleSchema, stringify, isEmptyValue } from "core/utils"
|
||||||
|
|
||||||
function getDefaultRequestBodyValue(requestBody, mediaType, activeExamplesKey) {
|
function getDefaultRequestBodyValue(requestBody, mediaType, activeExamplesKey) {
|
||||||
let mediaTypeValue = requestBody.getIn(["content", mediaType])
|
const mediaTypeValue = requestBody.getIn(["content", mediaType])
|
||||||
let schema = mediaTypeValue.get("schema").toJS()
|
const schema = mediaTypeValue.get("schema").toJS()
|
||||||
let example =
|
|
||||||
mediaTypeValue.get("example") !== undefined
|
|
||||||
? stringify(mediaTypeValue.get("example"))
|
|
||||||
: null
|
|
||||||
let currentExamplesValue = mediaTypeValue.getIn([
|
|
||||||
"examples",
|
|
||||||
activeExamplesKey,
|
|
||||||
"value"
|
|
||||||
])
|
|
||||||
|
|
||||||
if (mediaTypeValue.get("examples")) {
|
const hasExamplesKey = mediaTypeValue.get("examples") !== undefined
|
||||||
// the media type DOES have examples
|
const exampleSchema = mediaTypeValue.get("example")
|
||||||
return stringify(currentExamplesValue) || ""
|
const mediaTypeExample = hasExamplesKey
|
||||||
} else {
|
? mediaTypeValue.getIn([
|
||||||
// the media type DOES NOT have examples
|
"examples",
|
||||||
return stringify(
|
activeExamplesKey,
|
||||||
example ||
|
"value"
|
||||||
getSampleSchema(schema, mediaType, {
|
])
|
||||||
includeWriteOnly: true
|
: exampleSchema
|
||||||
}) ||
|
|
||||||
""
|
const exampleValue = getSampleSchema(
|
||||||
)
|
schema,
|
||||||
}
|
mediaType,
|
||||||
|
{
|
||||||
|
includeWriteOnly: true
|
||||||
|
},
|
||||||
|
mediaTypeExample
|
||||||
|
)
|
||||||
|
return stringify(exampleValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -212,6 +209,12 @@ const RequestBody = ({
|
|||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sampleRequestBody = getDefaultRequestBodyValue(
|
||||||
|
requestBody,
|
||||||
|
contentType,
|
||||||
|
activeExamplesKey,
|
||||||
|
)
|
||||||
|
|
||||||
return <div>
|
return <div>
|
||||||
{ requestBodyDescription &&
|
{ requestBodyDescription &&
|
||||||
<Markdown source={requestBodyDescription} />
|
<Markdown source={requestBodyDescription} />
|
||||||
@@ -235,11 +238,7 @@ const RequestBody = ({
|
|||||||
<RequestBodyEditor
|
<RequestBodyEditor
|
||||||
value={requestBodyValue}
|
value={requestBodyValue}
|
||||||
errors={requestBodyErrors}
|
errors={requestBodyErrors}
|
||||||
defaultValue={getDefaultRequestBodyValue(
|
defaultValue={sampleRequestBody}
|
||||||
requestBody,
|
|
||||||
contentType,
|
|
||||||
activeExamplesKey,
|
|
||||||
)}
|
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
getComponent={getComponent}
|
getComponent={getComponent}
|
||||||
/>
|
/>
|
||||||
@@ -257,11 +256,7 @@ const RequestBody = ({
|
|||||||
<HighlightCode
|
<HighlightCode
|
||||||
className="body-param__example"
|
className="body-param__example"
|
||||||
getConfigs={getConfigs}
|
getConfigs={getConfigs}
|
||||||
value={stringify(requestBodyValue) || getDefaultRequestBodyValue(
|
value={stringify(requestBodyValue) || sampleRequestBody}
|
||||||
requestBody,
|
|
||||||
contentType,
|
|
||||||
activeExamplesKey,
|
|
||||||
)}
|
|
||||||
/>
|
/>
|
||||||
}
|
}
|
||||||
includeWriteOnly={true}
|
includeWriteOnly={true}
|
||||||
|
|||||||
57
test/e2e-cypress/static/documents/bugs/6475.yaml
Normal file
57
test/e2e-cypress/static/documents/bugs/6475.yaml
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
openapi: 3.0.1
|
||||||
|
info:
|
||||||
|
title: Example Swagger
|
||||||
|
version: '1.0'
|
||||||
|
servers:
|
||||||
|
- url: /api/v1
|
||||||
|
paths:
|
||||||
|
/xmlTest/examples:
|
||||||
|
post:
|
||||||
|
summary: sample issues
|
||||||
|
operationId: xmlTest_examples
|
||||||
|
parameters: []
|
||||||
|
requestBody:
|
||||||
|
description: Simple Test xml examples
|
||||||
|
content:
|
||||||
|
application/xml:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Test"
|
||||||
|
examples:
|
||||||
|
test:
|
||||||
|
value:
|
||||||
|
x: should be xml
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Simple Test xml examples
|
||||||
|
content: {}
|
||||||
|
/xmlTest/example:
|
||||||
|
post:
|
||||||
|
summary: sample issues
|
||||||
|
operationId: xmlTest_example
|
||||||
|
parameters: []
|
||||||
|
requestBody:
|
||||||
|
description: Simple Test xml example
|
||||||
|
content:
|
||||||
|
application/xml:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/Test"
|
||||||
|
example:
|
||||||
|
x: should be xml
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: Simple Test xml example
|
||||||
|
content: {}
|
||||||
|
components:
|
||||||
|
schemas:
|
||||||
|
Test:
|
||||||
|
type: object
|
||||||
|
xml:
|
||||||
|
name: root
|
||||||
|
properties:
|
||||||
|
x:
|
||||||
|
type: string
|
||||||
|
other:
|
||||||
|
type: string
|
||||||
|
format: email
|
||||||
|
example:
|
||||||
|
x: what the f
|
||||||
65
test/e2e-cypress/tests/bugs/6475.js
Normal file
65
test/e2e-cypress/tests/bugs/6475.js
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
describe("#6475: 'Examples' keyword definitions can not be rendered as xml", () => {
|
||||||
|
it("should render requestBody examples preview accourdingly to content-type xml", () => {
|
||||||
|
const xmlIndicator = "<x>should be xml</x>"
|
||||||
|
|
||||||
|
cy
|
||||||
|
.visit("?url=/documents/bugs/6475.yaml")
|
||||||
|
.get("#operations-default-xmlTest_examples")
|
||||||
|
.click()
|
||||||
|
.get(".opblock-section-request-body")
|
||||||
|
.within(() => {
|
||||||
|
cy
|
||||||
|
.get(".microlight")
|
||||||
|
.should("include.text", xmlIndicator)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
it("should requestBody examples input accourdingly to content-type xml", () => {
|
||||||
|
const xmlIndicator = "<x>should be xml</x>"
|
||||||
|
|
||||||
|
cy
|
||||||
|
.visit("?url=/documents/bugs/6475.yaml")
|
||||||
|
.get("#operations-default-xmlTest_examples")
|
||||||
|
.click()
|
||||||
|
.get(".btn.try-out__btn")
|
||||||
|
.click()
|
||||||
|
.get(".opblock-section-request-body")
|
||||||
|
.within(() => {
|
||||||
|
cy
|
||||||
|
.get("textarea")
|
||||||
|
.contains(xmlIndicator)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("#6475: 'Example' keyword definitions can not be rendered as xml", () => {
|
||||||
|
it("should render requestBody examples preview accourdingly to content-type xml", () => {
|
||||||
|
const xmlIndicator = "<x>should be xml</x>"
|
||||||
|
|
||||||
|
cy
|
||||||
|
.visit("?url=/documents/bugs/6475.yaml")
|
||||||
|
.get("#operations-default-xmlTest_example")
|
||||||
|
.click()
|
||||||
|
.get(".opblock-section-request-body")
|
||||||
|
.within(() => {
|
||||||
|
cy
|
||||||
|
.get(".microlight")
|
||||||
|
.should("include.text", xmlIndicator)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
it("should requestBody examples input accourdingly to content-type xml", () => {
|
||||||
|
const xmlIndicator = "<x>should be xml</x>"
|
||||||
|
|
||||||
|
cy
|
||||||
|
.visit("?url=/documents/bugs/6475.yaml")
|
||||||
|
.get("#operations-default-xmlTest_example")
|
||||||
|
.click()
|
||||||
|
.get(".btn.try-out__btn")
|
||||||
|
.click()
|
||||||
|
.get(".opblock-section-request-body")
|
||||||
|
.within(() => {
|
||||||
|
cy
|
||||||
|
.get("textarea")
|
||||||
|
.contains(xmlIndicator)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user