Skip to content

Commit 373da3b

Browse files
authored
fix: provide JSON editor for x-www-form-urlencoded bodies lacking properties (via #5180)
1 parent 3f0c066 commit 373da3b

File tree

5 files changed

+102
-9
lines changed

5 files changed

+102
-9
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const RequestBody = ({
3030
const requestBodyContent = (requestBody && requestBody.get("content")) || new OrderedMap()
3131
contentType = contentType || requestBodyContent.keySeq().first()
3232

33-
const mediaTypeValue = requestBodyContent.get(contentType)
33+
const mediaTypeValue = requestBodyContent.get(contentType, OrderedMap())
34+
const schemaForMediaType = mediaTypeValue.get("schema", OrderedMap())
3435

3536
if(!mediaTypeValue) {
3637
return null
@@ -55,15 +56,17 @@ const RequestBody = ({
5556
return <Input type={"file"} onChange={handleFile} />
5657
}
5758

58-
if(
59+
if (
5960
isObjectContent &&
60-
(contentType === "application/x-www-form-urlencoded"
61-
|| contentType.indexOf("multipart/") === 0))
62-
{
61+
(
62+
contentType === "application/x-www-form-urlencoded" ||
63+
contentType.indexOf("multipart/") === 0
64+
) &&
65+
schemaForMediaType.get("properties", OrderedMap()).size > 0
66+
) {
6367
const JsonSchemaForm = getComponent("JsonSchemaForm")
6468
const ParameterExt = getComponent("ParameterExt")
65-
const schemaForContentType = requestBody.getIn(["content", contentType, "schema"], OrderedMap())
66-
const bodyProperties = schemaForContentType.getIn([ "properties"], OrderedMap())
69+
const bodyProperties = schemaForMediaType.get("properties", OrderedMap())
6770
requestBodyValue = Map.isMap(requestBodyValue) ? requestBodyValue : OrderedMap()
6871

6972
return <div className="table-container">
@@ -75,7 +78,7 @@ const RequestBody = ({
7578
{
7679
bodyProperties.map((prop, key) => {
7780
let commonExt = showCommonExtensions ? getCommonExtensions(prop) : null
78-
const required = schemaForContentType.get("required", List()).includes(key)
81+
const required = schemaForMediaType.get("required", List()).includes(key)
7982
const type = prop.get("type")
8083
const format = prop.get("format")
8184
const description = prop.get("description")

src/core/plugins/oas3/wrap-components/parameters.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class Parameters extends Component {
171171
</div> : "" }
172172
{
173173
isOAS3() && requestBody && this.state.parametersVisible &&
174-
<div className="opblock-section">
174+
<div className="opblock-section opblock-section-request-body">
175175
<div className="opblock-section-header">
176176
<h4 className={`opblock-title parameter__name ${requestBody.get("required") && "required"}`}>Request body</h4>
177177
<label>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
openapi: "3.0.0"
2+
info:
3+
description: "A sample API for "
4+
version: "1.0.0"
5+
title: "Sample"
6+
contact:
7+
name: ""
8+
url: "http://website.com"
9+
10+
paths:
11+
/:
12+
post:
13+
summary: Create/modify object
14+
operationId: postObject
15+
parameters:
16+
- name: filterParams
17+
in: query
18+
description: Additional filter fields
19+
required: false
20+
schema:
21+
type: object
22+
requestBody:
23+
content:
24+
application/x-www-form-urlencoded:
25+
schema:
26+
type: object
27+
additionalProperties:
28+
type: string
29+
responses:
30+
'200':
31+
description: Status message
32+
content:
33+
application/json:
34+
schema:
35+
type: object
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: "3.0.0"
2+
info:
3+
description: "A sample API for "
4+
version: "1.0.0"
5+
title: "Sample"
6+
contact:
7+
name: ""
8+
url: "http://website.com"
9+
10+
paths:
11+
/:
12+
post:
13+
summary: Create/modify object
14+
operationId: postObject
15+
parameters:
16+
- name: filterParams
17+
in: query
18+
description: Additional filter fields
19+
required: false
20+
schema:
21+
type: object
22+
requestBody:
23+
content:
24+
application/x-www-form-urlencoded:
25+
schema:
26+
type: object
27+
responses:
28+
'200':
29+
description: Status message
30+
content:
31+
application/json:
32+
schema:
33+
type: object
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
describe("#5072: x-www-form-urlencoded request body input when `properties` is missing", () => {
2+
it("should provide a JSON input for an empty object schema", () => {
3+
cy
4+
.visit("?url=/documents/bugs/5072/empty.yaml")
5+
.get("#operations-default-postObject")
6+
.click()
7+
.get(".try-out__btn")
8+
.click()
9+
.get(`.opblock-section-request-body textarea`)
10+
.should("have.value", "{}")
11+
})
12+
it("should provide a JSON input for an additionalProperties object schema", () => {
13+
cy
14+
.visit("?url=/documents/bugs/5072/additional.yaml")
15+
.get("#operations-default-postObject")
16+
.click()
17+
.get(".try-out__btn")
18+
.click()
19+
.get(`.opblock-section-request-body textarea`)
20+
.contains(`"additionalProp1": "string"`)
21+
})
22+
})

0 commit comments

Comments
 (0)