Skip to content
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

fix: add nullable condition to multipart formdata for openapi3.1 #1646

Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 7 additions & 7 deletions packages/core/src/getters/res-req-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,18 +416,18 @@ const resolveSchemaPropertiesToFormData = ({
if (itemSchema.type === 'object' || itemSchema.type === 'array') {
valueStr = 'JSON.stringify(value)';
} else if (
itemSchema.type === 'number' ||
itemSchema.type === 'integer' ||
itemSchema.type === 'boolean'
itemSchema.type === 'number' || itemSchema.type?.includes('number') ||
itemSchema.type === 'integer' || itemSchema.type?.includes('integer') ||
itemSchema.type === 'boolean' || itemSchema.type?.includes('boolean')
) {
valueStr = 'value.toString()';
}
}
formDataValue = `${valueKey}.forEach(value => ${variableName}.append('${key}', ${valueStr}));\n`;
} else if (
property.type === 'number' ||
property.type === 'integer' ||
property.type === 'boolean'
property.type === 'number' || property.type?.includes('number') ||
property.type === 'integer' || property.type?.includes('integer') ||
property.type === 'boolean' || property.type?.includes('boolean')
) {
formDataValue = `${variableName}.append('${key}', ${nonOptionalValueKey}.toString())\n`;
} else {
Expand All @@ -437,7 +437,7 @@ const resolveSchemaPropertiesToFormData = ({
const isRequired =
schema.required?.includes(key) && !isRequestBodyOptional;

if (property.nullable) {
if (property.nullable || property.type?.includes('null')) {
if (isRequired) {
return acc + `if(${valueKey} !== null) {\n ${formDataValue} }\n`;
}
Expand Down
33 changes: 33 additions & 0 deletions tests/specifications/null-type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,24 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/NullableObject'
/nullable-with-multipart-form-data:
post:
tags:
- nullables
summary: Nullable with multipart/form-data request
operationId: NullableWithMultipartFormRequest
requestBody:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/MultipartFormDataNullable'
responses:
200:
description: Successful Operation
content:
application/json:
schema:
$ref: '#/components/schemas/MultipartFormDataNullable'
components:
schemas:
NullableObject:
Expand All @@ -62,3 +80,18 @@ components:
BlankEnum:
enum:
- ''
MultipartFormDataNullable:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I fixed it.
I also found a bug when oneOf, anyOf, allOf subschemas are attached and fixed it.

type: object
properties:
name:
type:
- 'string'
- 'null'
age:
type:
- 'integer'
- 'null'
is_active:
type:
- 'boolean'
- 'null'