Skip to content

Commit 2696730

Browse files
authored
feat(oas31): display file upload input when contentMediaType or contentEncoding is present (#10412)
Refs #9278
1 parent c29e712 commit 2696730

File tree

3 files changed

+146
-1
lines changed

3 files changed

+146
-1
lines changed

src/core/plugins/oas31/oas3-extensions/fn.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
11
/**
22
* @prettier
33
*/
4+
import { Map } from "immutable"
5+
import isPlainObject from "lodash/isPlainObject"
46

57
export const makeIsFileUploadIntended = (getSystem) => {
68
const isFileUploadIntended = (schema, mediaType = null) => {
79
const { fn } = getSystem()
810

9-
return fn.isFileUploadIntendedOAS30(schema, mediaType)
11+
/**
12+
* Return `true` early if the media type indicates a file upload
13+
* or if a combination of type: `string` and format: `binary/byte` is detected.
14+
* This ensures support for empty Media Type Objects,
15+
* as the schema check is performed later.
16+
*/
17+
const isFileUploadIntendedOAS30 = fn.isFileUploadIntendedOAS30(
18+
schema,
19+
mediaType
20+
)
21+
22+
if (isFileUploadIntendedOAS30) {
23+
return true
24+
}
25+
26+
const isSchemaImmutable = Map.isMap(schema)
27+
28+
if (!isSchemaImmutable && !isPlainObject(schema)) {
29+
return false
30+
}
31+
32+
const contentMediaType = isSchemaImmutable
33+
? schema.get("contentMediaType")
34+
: schema.contentMediaType
35+
const contentEncoding = isSchemaImmutable
36+
? schema.get("contentEncoding")
37+
: schema.contentEncoding
38+
39+
return (
40+
(typeof contentMediaType === "string" && contentMediaType !== "") ||
41+
(typeof contentEncoding === "string" && contentEncoding !== "")
42+
)
1043
}
1144

1245
return isFileUploadIntended

test/e2e-cypress/e2e/features/plugins/oas31/oas31-request-body-upload-file.cy.js

+70
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,50 @@ describe("OpenAPI 3.1 Request Body upload file button", () => {
199199
})
200200
})
201201

202+
describe("schema contentMediaType is a non-empty string", () => {
203+
beforeEach(() => {
204+
cy.get("#operations-default-uploadSchemaContentMediaType").click()
205+
})
206+
207+
it("should display description with the correct content type", () => {
208+
cy.get(
209+
".opblock-section-request-body .opblock-description-wrapper i"
210+
).should(
211+
"have.text",
212+
"Example values are not available for application/x-custom media types."
213+
)
214+
})
215+
216+
it("should display a file upload button", () => {
217+
cy.get(".try-out__btn").click()
218+
cy.get(
219+
".opblock-section-request-body .opblock-description-wrapper input"
220+
).should("have.prop", "type", "file")
221+
})
222+
})
223+
224+
describe("schema contentEncoding is a non-empty string", () => {
225+
beforeEach(() => {
226+
cy.get("#operations-default-uploadSchemaContentEncoding").click()
227+
})
228+
229+
it("should display description with the correct content type", () => {
230+
cy.get(
231+
".opblock-section-request-body .opblock-description-wrapper i"
232+
).should(
233+
"have.text",
234+
"Example values are not available for application/x-custom media types."
235+
)
236+
})
237+
238+
it("should display a file upload button", () => {
239+
cy.get(".try-out__btn").click()
240+
cy.get(
241+
".opblock-section-request-body .opblock-description-wrapper input"
242+
).should("have.prop", "type", "file")
243+
})
244+
})
245+
202246
describe("multipart/form-data object property with schema type string and format binary", () => {
203247
beforeEach(() => {
204248
cy.get("#operations-default-uploadPropertySchemaFormatBinary").click()
@@ -254,4 +298,30 @@ describe("OpenAPI 3.1 Request Body upload file button", () => {
254298
).should("have.prop", "type", "file")
255299
})
256300
})
301+
302+
describe("multipart/form-data object property schema has contentMediaType with non-empty string", () => {
303+
beforeEach(() => {
304+
cy.get("#operations-default-uploadPropertySchemaContentMediaType").click()
305+
})
306+
307+
it("should display a file upload button", () => {
308+
cy.get(".try-out__btn").click()
309+
cy.get(
310+
".opblock-section-request-body .opblock-description-wrapper input"
311+
).should("have.prop", "type", "file")
312+
})
313+
})
314+
315+
describe("multipart/form-data object property schema has contentEncoding with non-empty string", () => {
316+
beforeEach(() => {
317+
cy.get("#operations-default-uploadPropertySchemaContentEncoding").click()
318+
})
319+
320+
it("should display a file upload button", () => {
321+
cy.get(".try-out__btn").click()
322+
cy.get(
323+
".opblock-section-request-body .opblock-description-wrapper input"
324+
).should("have.prop", "type", "file")
325+
})
326+
})
257327
})

test/e2e-cypress/static/documents/features/oas31-request-body-upload-file.yaml

+42
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ info:
1212
* schema type is `string` and format is `binary` (no matter what content type)
1313
* schema union type includes `string` and format is `byte` (no matter what content type)
1414
* schema union type includes `string` and format is `binary` (no matter what content type)
15+
* schema `contentMediaType` is present and is a non-empty `string`
16+
* schema `contentEncoding` is present and is a non-empty `string`
1517
* multipart/form-data object property schema type is `string` and format is `byte`
1618
* multipart/form-data object property schema type is `string` and format is `binary`
1719
* multipart/form-data object property schema union type includes `string` and format is `byte`
1820
* multipart/form-data object property schema union type includes `string` and format is `binary`
21+
* multipart/form-data object property schema `contentMediaType` is present and is a non-empty `string`
22+
* multipart/form-data object property schema `contentEncoding` is present and is a non-empty `string`
1923
version: "1.0.0"
2024
paths:
2125
/upload-application-octet-stream:
@@ -96,6 +100,22 @@ paths:
96100
- object
97101
- string
98102
format: byte
103+
/upload-schema-contentMediaType:
104+
post:
105+
operationId: uploadSchemaContentMediaType
106+
requestBody:
107+
content:
108+
application/x-custom:
109+
schema:
110+
contentMediaType: application/x-custom
111+
/upload-schema-contentEncoding:
112+
post:
113+
operationId: uploadSchemaContentEncoding
114+
requestBody:
115+
content:
116+
application/x-custom:
117+
schema:
118+
contentEncoding: base64
99119
/upload-property-schema-format-binary:
100120
post:
101121
operationId: uploadPropertySchemaFormatBinary
@@ -148,3 +168,25 @@ paths:
148168
- object
149169
- string
150170
format: byte
171+
/upload-property-schema-contentMediaType:
172+
post:
173+
operationId: uploadPropertySchemaContentMediaType
174+
requestBody:
175+
content:
176+
multipart/form-data:
177+
schema:
178+
type: object
179+
properties:
180+
file:
181+
contentMediaType: application/x-custom
182+
/upload-property-schema-contentEncoding:
183+
post:
184+
operationId: uploadPropertySchemaContentEncoding
185+
requestBody:
186+
content:
187+
multipart/form-data:
188+
schema:
189+
type: object
190+
properties:
191+
file:
192+
contentEncoding: base64

0 commit comments

Comments
 (0)