diff --git a/packages/core/src/getters/res-req-types.ts b/packages/core/src/getters/res-req-types.ts index 6b354dc7d..d3d920566 100644 --- a/packages/core/src/getters/res-req-types.ts +++ b/packages/core/src/getters/res-req-types.ts @@ -417,8 +417,11 @@ 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()'; } @@ -426,18 +429,48 @@ const resolveSchemaPropertiesToFormData = ({ 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`; } diff --git a/tests/specifications/null-type.yaml b/tests/specifications/null-type.yaml index 512e5e9b6..a472d7c03 100644 --- a/tests/specifications/null-type.yaml +++ b/tests/specifications/null-type.yaml @@ -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: @@ -54,6 +72,10 @@ components: oneOf: - type: 'string' - type: 'null' + is_active: + type: + - 'boolean' + - 'null' NullEnum: nullable: true enum: