Skip to content

Commit 68e9b1b

Browse files
authored
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
1 parent f35acce commit 68e9b1b

File tree

3 files changed

+151
-34
lines changed

3 files changed

+151
-34
lines changed

src/core/plugins/oas3/components/request-body.jsx

+29-34
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,28 @@ import { Map, OrderedMap, List } from "immutable"
55
import { getCommonExtensions, getSampleSchema, stringify, isEmptyValue } from "core/utils"
66

77
function getDefaultRequestBodyValue(requestBody, mediaType, activeExamplesKey) {
8-
let mediaTypeValue = requestBody.getIn(["content", mediaType])
9-
let schema = mediaTypeValue.get("schema").toJS()
10-
let example =
11-
mediaTypeValue.get("example") !== undefined
12-
? stringify(mediaTypeValue.get("example"))
13-
: null
14-
let currentExamplesValue = mediaTypeValue.getIn([
15-
"examples",
16-
activeExamplesKey,
17-
"value"
18-
])
8+
const mediaTypeValue = requestBody.getIn(["content", mediaType])
9+
const schema = mediaTypeValue.get("schema").toJS()
1910

20-
if (mediaTypeValue.get("examples")) {
21-
// the media type DOES have examples
22-
return stringify(currentExamplesValue) || ""
23-
} else {
24-
// the media type DOES NOT have examples
25-
return stringify(
26-
example ||
27-
getSampleSchema(schema, mediaType, {
28-
includeWriteOnly: true
29-
}) ||
30-
""
31-
)
32-
}
11+
const hasExamplesKey = mediaTypeValue.get("examples") !== undefined
12+
const exampleSchema = mediaTypeValue.get("example")
13+
const mediaTypeExample = hasExamplesKey
14+
? mediaTypeValue.getIn([
15+
"examples",
16+
activeExamplesKey,
17+
"value"
18+
])
19+
: exampleSchema
20+
21+
const exampleValue = getSampleSchema(
22+
schema,
23+
mediaType,
24+
{
25+
includeWriteOnly: true
26+
},
27+
mediaTypeExample
28+
)
29+
return stringify(exampleValue)
3330
}
3431

3532

@@ -212,6 +209,12 @@ const RequestBody = ({
212209
</div>
213210
}
214211

212+
const sampleRequestBody = getDefaultRequestBodyValue(
213+
requestBody,
214+
contentType,
215+
activeExamplesKey,
216+
)
217+
215218
return <div>
216219
{ requestBodyDescription &&
217220
<Markdown source={requestBodyDescription} />
@@ -235,11 +238,7 @@ const RequestBody = ({
235238
<RequestBodyEditor
236239
value={requestBodyValue}
237240
errors={requestBodyErrors}
238-
defaultValue={getDefaultRequestBodyValue(
239-
requestBody,
240-
contentType,
241-
activeExamplesKey,
242-
)}
241+
defaultValue={sampleRequestBody}
243242
onChange={onChange}
244243
getComponent={getComponent}
245244
/>
@@ -257,11 +256,7 @@ const RequestBody = ({
257256
<HighlightCode
258257
className="body-param__example"
259258
getConfigs={getConfigs}
260-
value={stringify(requestBodyValue) || getDefaultRequestBodyValue(
261-
requestBody,
262-
contentType,
263-
activeExamplesKey,
264-
)}
259+
value={stringify(requestBodyValue) || sampleRequestBody}
265260
/>
266261
}
267262
includeWriteOnly={true}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Example Swagger
4+
version: '1.0'
5+
servers:
6+
- url: /api/v1
7+
paths:
8+
/xmlTest/examples:
9+
post:
10+
summary: sample issues
11+
operationId: xmlTest_examples
12+
parameters: []
13+
requestBody:
14+
description: Simple Test xml examples
15+
content:
16+
application/xml:
17+
schema:
18+
$ref: "#/components/schemas/Test"
19+
examples:
20+
test:
21+
value:
22+
x: should be xml
23+
responses:
24+
'200':
25+
description: Simple Test xml examples
26+
content: {}
27+
/xmlTest/example:
28+
post:
29+
summary: sample issues
30+
operationId: xmlTest_example
31+
parameters: []
32+
requestBody:
33+
description: Simple Test xml example
34+
content:
35+
application/xml:
36+
schema:
37+
$ref: "#/components/schemas/Test"
38+
example:
39+
x: should be xml
40+
responses:
41+
'200':
42+
description: Simple Test xml example
43+
content: {}
44+
components:
45+
schemas:
46+
Test:
47+
type: object
48+
xml:
49+
name: root
50+
properties:
51+
x:
52+
type: string
53+
other:
54+
type: string
55+
format: email
56+
example:
57+
x: what the f

test/e2e-cypress/tests/bugs/6475.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
describe("#6475: 'Examples' keyword definitions can not be rendered as xml", () => {
2+
it("should render requestBody examples preview accourdingly to content-type xml", () => {
3+
const xmlIndicator = "<x>should be xml</x>"
4+
5+
cy
6+
.visit("?url=/documents/bugs/6475.yaml")
7+
.get("#operations-default-xmlTest_examples")
8+
.click()
9+
.get(".opblock-section-request-body")
10+
.within(() => {
11+
cy
12+
.get(".microlight")
13+
.should("include.text", xmlIndicator)
14+
})
15+
})
16+
it("should requestBody examples input accourdingly to content-type xml", () => {
17+
const xmlIndicator = "<x>should be xml</x>"
18+
19+
cy
20+
.visit("?url=/documents/bugs/6475.yaml")
21+
.get("#operations-default-xmlTest_examples")
22+
.click()
23+
.get(".btn.try-out__btn")
24+
.click()
25+
.get(".opblock-section-request-body")
26+
.within(() => {
27+
cy
28+
.get("textarea")
29+
.contains(xmlIndicator)
30+
})
31+
})
32+
})
33+
34+
describe("#6475: 'Example' keyword definitions can not be rendered as xml", () => {
35+
it("should render requestBody examples preview accourdingly to content-type xml", () => {
36+
const xmlIndicator = "<x>should be xml</x>"
37+
38+
cy
39+
.visit("?url=/documents/bugs/6475.yaml")
40+
.get("#operations-default-xmlTest_example")
41+
.click()
42+
.get(".opblock-section-request-body")
43+
.within(() => {
44+
cy
45+
.get(".microlight")
46+
.should("include.text", xmlIndicator)
47+
})
48+
})
49+
it("should requestBody examples input accourdingly to content-type xml", () => {
50+
const xmlIndicator = "<x>should be xml</x>"
51+
52+
cy
53+
.visit("?url=/documents/bugs/6475.yaml")
54+
.get("#operations-default-xmlTest_example")
55+
.click()
56+
.get(".btn.try-out__btn")
57+
.click()
58+
.get(".opblock-section-request-body")
59+
.within(() => {
60+
cy
61+
.get("textarea")
62+
.contains(xmlIndicator)
63+
})
64+
})
65+
})

0 commit comments

Comments
 (0)