Skip to content

Commit

Permalink
fix: add nullable condition to multipart formdata for openapi3.1 (#1646)
Browse files Browse the repository at this point in the history
* fix: add nullable condition to multipart formdata for openapi3.1

* fix: add multipart form-data nullable test case

* fix: multipart formdata type must be string

* fix: test case and sub schema bugfix

* chore: prettier format

* chore: pluralize variable name
  • Loading branch information
kosa3 authored Sep 30, 2024
1 parent 535bb65 commit b3b0306
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
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

0 comments on commit b3b0306

Please sign in to comment.