Skip to content

Commit

Permalink
Improved null type handling in OpenAPI generator
Browse files Browse the repository at this point in the history
  • Loading branch information
gius committed Feb 20, 2024
1 parent fdbf53a commit 247238f
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions packages/generator/src/openapi/parsers/openApi3Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })),
};
}
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 247238f

Please sign in to comment.