From 247238f79c56f0653912dfcf60b273dc41f72130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Augustin=20=C5=A0ulc?= Date: Tue, 20 Feb 2024 10:22:03 +0100 Subject: [PATCH] Improved null type handling in OpenAPI generator --- .../src/openapi/parsers/openApi3Parser.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/generator/src/openapi/parsers/openApi3Parser.ts b/packages/generator/src/openapi/parsers/openApi3Parser.ts index fe5369d..889070c 100644 --- a/packages/generator/src/openapi/parsers/openApi3Parser.ts +++ b/packages/generator/src/openapi/parsers/openApi3Parser.ts @@ -123,18 +123,17 @@ export default class OpenApi3Parser implements ApiModel { } if (Array.isArray(definition.type) && definition.type.length > 0) { - let types = definition.type as string[]; + const concreteTypes = (definition.type as string[]).filter(x => !isNullType(x)); - if (definition.type.includes("null")) { + if (concreteTypes.length !== definition.type.length) { definition.nullable = true; - types = types.filter(x => x !== "null"); // the current definition does not support arrays } - if (types.length == 1) { - definition.type = types[0] as OpenAPIV3.NonArraySchemaObjectType; + if (concreteTypes.length == 1) { + definition.type = concreteTypes[0] as OpenAPIV3.NonArraySchemaObjectType; } else { return { - oneOf: types.map(x => ({ ...definition, type: x as OpenAPIV3.NonArraySchemaObjectType })), + oneOf: concreteTypes.map(x => ({ ...definition, type: x as OpenAPIV3.NonArraySchemaObjectType })), }; } } @@ -418,8 +417,8 @@ function cleanParameterName(name: string) { } } -function isNullType(type: string | undefined) { - return type === "null"; +function isNullType(type: string | string[] | undefined): boolean { + return type === "null" || (Array.isArray(type) && type.length === 1 && isNullType(type[0])); } function isNullObject(definition: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject) {