Skip to content

Commit

Permalink
fix: test case and sub schema bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
kosa3 committed Sep 30, 2024
1 parent 4ed6538 commit 2c57e25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
19 changes: 18 additions & 1 deletion packages/core/src/getters/res-req-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,27 @@ const resolveSchemaPropertiesToFormData = ({
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 subSchema = combine?.map((c) => resolveObject({schema: c, combined: true, context: context}))
if (subSchema?.some(schema => {
return ['number', 'integer', 'boolean'].includes(schema.type)
})) {
formDataValue = `${variableName}.append('${key}', ${nonOptionalValueKey}.toString())\n`;
}

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

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

if (property.nullable || property.type?.includes('null')) {
if (property.nullable || property.type?.includes('null') || existSubSchemaNullable) {
if (isRequired) {
return acc + `if(${valueKey} !== null) {\n ${formDataValue} }\n`;
}
Expand Down
23 changes: 6 additions & 17 deletions tests/specifications/null-type.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ paths:
content:
multipart/form-data:
schema:
$ref: '#/components/schemas/MultipartFormDataNullable'
$ref: '#/components/schemas/NullableObject'
responses:
200:
description: Successful Operation
content:
application/json:
schema:
$ref: '#/components/schemas/MultipartFormDataNullable'
$ref: '#/components/schemas/NullableObject'
components:
schemas:
NullableObject:
Expand All @@ -72,6 +72,10 @@ components:
oneOf:
- type: 'string'
- type: 'null'
is_active:
type:
- 'boolean'
- 'null'
NullEnum:
nullable: true
enum:
Expand All @@ -80,18 +84,3 @@ components:
BlankEnum:
enum:
- ''
MultipartFormDataNullable:
type: object
properties:
name:
type:
- 'string'
- 'null'
age:
type:
- 'integer'
- 'null'
is_active:
type:
- 'boolean'
- 'null'

0 comments on commit 2c57e25

Please sign in to comment.