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 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
39 changes: 36 additions & 3 deletions packages/core/src/getters/res-req-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,27 +417,60 @@ const resolveSchemaPropertiesToFormData = ({
valueStr = 'JSON.stringify(value)';
} else if (
itemSchema.type === 'number' ||
itemSchema.type?.includes('number') ||
itemSchema.type === 'integer' ||
itemSchema.type === 'boolean'
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?.includes('number') ||
property.type === 'integer' ||
property.type === 'boolean'
property.type?.includes('integer') ||
property.type === 'boolean' ||
property.type?.includes('boolean')
) {
formDataValue = `${variableName}.append('${key}', ${nonOptionalValueKey}.toString())\n`;
} else {
formDataValue = `${variableName}.append('${key}', ${nonOptionalValueKey})\n`;
}

let existSubSchemaNullable = false;
if (property.allOf || property.anyOf || property.oneOf) {
const combine = property.allOf || property.anyOf || property.oneOf;
const subSchemas = combine?.map((c) =>
resolveObject({ schema: c, combined: true, context: context }),
);
if (
subSchemas?.some((subSchema) => {
return ['number', 'integer', 'boolean'].includes(subSchema.type);
})
) {
formDataValue = `${variableName}.append('${key}', ${nonOptionalValueKey}.toString())\n`;
}

if (
subSchemas?.some((subSchema) => {
return subSchema.type === 'null';
})
) {
existSubSchemaNullable = true;
}
}

const isRequired =
schema.required?.includes(key) && !isRequestBodyOptional;

if (property.nullable) {
if (
property.nullable ||
property.type?.includes('null') ||
existSubSchemaNullable
) {
if (isRequired) {
return acc + `if(${valueKey} !== null) {\n ${formDataValue} }\n`;
}
Expand Down
22 changes: 22 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/NullableObject'
responses:
200:
description: Successful Operation
content:
application/json:
schema:
$ref: '#/components/schemas/NullableObject'
components:
schemas:
NullableObject:
Expand All @@ -54,6 +72,10 @@ components:
oneOf:
- type: 'string'
- type: 'null'
is_active:
type:
- 'boolean'
- 'null'
NullEnum:
nullable: true
enum:
Expand Down