Skip to content

feat(oas31): display file upload input when contentMediaType or contentEncoding is present #10412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/core/plugins/oas31/oas3-extensions/fn.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
/**
* @prettier
*/
import { Map } from "immutable"
import isPlainObject from "lodash/isPlainObject"

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

return fn.isFileUploadIntendedOAS30(schema, mediaType)
/**
* Return `true` early if the media type indicates a file upload
* or if a combination of type: `string` and format: `binary/byte` is detected.
* This ensures support for empty Media Type Objects,
* as the schema check is performed later.
*/
const isFileUploadIntendedOAS30 = fn.isFileUploadIntendedOAS30(
schema,
mediaType
)

if (isFileUploadIntendedOAS30) {
return true
}

const isSchemaImmutable = Map.isMap(schema)

if (!isSchemaImmutable && !isPlainObject(schema)) {
return false
}

const contentMediaType = isSchemaImmutable
? schema.get("contentMediaType")
: schema.contentMediaType
const contentEncoding = isSchemaImmutable
? schema.get("contentEncoding")
: schema.contentEncoding

return (
(typeof contentMediaType === "string" && contentMediaType !== "") ||
(typeof contentEncoding === "string" && contentEncoding !== "")
)
}

return isFileUploadIntended
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,50 @@ describe("OpenAPI 3.1 Request Body upload file button", () => {
})
})

describe("schema contentMediaType is a non-empty string", () => {
beforeEach(() => {
cy.get("#operations-default-uploadSchemaContentMediaType").click()
})

it("should display description with the correct content type", () => {
cy.get(
".opblock-section-request-body .opblock-description-wrapper i"
).should(
"have.text",
"Example values are not available for application/x-custom media types."
)
})

it("should display a file upload button", () => {
cy.get(".try-out__btn").click()
cy.get(
".opblock-section-request-body .opblock-description-wrapper input"
).should("have.prop", "type", "file")
})
})

describe("schema contentEncoding is a non-empty string", () => {
beforeEach(() => {
cy.get("#operations-default-uploadSchemaContentEncoding").click()
})

it("should display description with the correct content type", () => {
cy.get(
".opblock-section-request-body .opblock-description-wrapper i"
).should(
"have.text",
"Example values are not available for application/x-custom media types."
)
})

it("should display a file upload button", () => {
cy.get(".try-out__btn").click()
cy.get(
".opblock-section-request-body .opblock-description-wrapper input"
).should("have.prop", "type", "file")
})
})

describe("multipart/form-data object property with schema type string and format binary", () => {
beforeEach(() => {
cy.get("#operations-default-uploadPropertySchemaFormatBinary").click()
Expand Down Expand Up @@ -254,4 +298,30 @@ describe("OpenAPI 3.1 Request Body upload file button", () => {
).should("have.prop", "type", "file")
})
})

describe("multipart/form-data object property schema has contentMediaType with non-empty string", () => {
beforeEach(() => {
cy.get("#operations-default-uploadPropertySchemaContentMediaType").click()
})

it("should display a file upload button", () => {
cy.get(".try-out__btn").click()
cy.get(
".opblock-section-request-body .opblock-description-wrapper input"
).should("have.prop", "type", "file")
})
})

describe("multipart/form-data object property schema has contentEncoding with non-empty string", () => {
beforeEach(() => {
cy.get("#operations-default-uploadPropertySchemaContentEncoding").click()
})

it("should display a file upload button", () => {
cy.get(".try-out__btn").click()
cy.get(
".opblock-section-request-body .opblock-description-wrapper input"
).should("have.prop", "type", "file")
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ info:
* schema type is `string` and format is `binary` (no matter what content type)
* schema union type includes `string` and format is `byte` (no matter what content type)
* schema union type includes `string` and format is `binary` (no matter what content type)
* schema `contentMediaType` is present and is a non-empty `string`
* schema `contentEncoding` is present and is a non-empty `string`
* multipart/form-data object property schema type is `string` and format is `byte`
* multipart/form-data object property schema type is `string` and format is `binary`
* multipart/form-data object property schema union type includes `string` and format is `byte`
* multipart/form-data object property schema union type includes `string` and format is `binary`
* multipart/form-data object property schema `contentMediaType` is present and is a non-empty `string`
* multipart/form-data object property schema `contentEncoding` is present and is a non-empty `string`
version: "1.0.0"
paths:
/upload-application-octet-stream:
Expand Down Expand Up @@ -96,6 +100,22 @@ paths:
- object
- string
format: byte
/upload-schema-contentMediaType:
post:
operationId: uploadSchemaContentMediaType
requestBody:
content:
application/x-custom:
schema:
contentMediaType: application/x-custom
/upload-schema-contentEncoding:
post:
operationId: uploadSchemaContentEncoding
requestBody:
content:
application/x-custom:
schema:
contentEncoding: base64
/upload-property-schema-format-binary:
post:
operationId: uploadPropertySchemaFormatBinary
Expand Down Expand Up @@ -148,3 +168,25 @@ paths:
- object
- string
format: byte
/upload-property-schema-contentMediaType:
post:
operationId: uploadPropertySchemaContentMediaType
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
file:
contentMediaType: application/x-custom
/upload-property-schema-contentEncoding:
post:
operationId: uploadPropertySchemaContentEncoding
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
file:
contentEncoding: base64