Skip to content

Commit 7d45d0d

Browse files
author
Daniel DeMicco
committed
fix: Support for generating enums when enums definition has null value
1 parent 2a4b067 commit 7d45d0d

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

.changeset/real-penguins-fold.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": minor
3+
---
4+
5+
Support for generating enums when enums definition has null value

packages/openapi-typescript/src/transform/schema-object.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,38 @@ export function transformSchemaObjectWithComposition(
9494
!("additionalProperties" in schemaObject)
9595
) {
9696
// hoist enum to top level if string/number enum and option is enabled
97-
if (options.ctx.enum && schemaObject.enum.every((v) => typeof v === "string" || typeof v === "number")) {
97+
if (
98+
options.ctx.enum &&
99+
schemaObject.enum.every((v) => typeof v === "string" || typeof v === "number" || v === null)
100+
) {
98101
let enumName = parseRef(options.path ?? "").pointer.join("/");
99102
// allow #/components/schemas to have simpler names
100103
enumName = enumName.replace("components/schemas", "");
101104
const metadata = schemaObject.enum.map((_, i) => ({
102105
name: schemaObject["x-enum-varnames"]?.[i] ?? schemaObject["x-enumNames"]?.[i],
103106
description: schemaObject["x-enum-descriptions"]?.[i] ?? schemaObject["x-enumDescriptions"]?.[i],
104107
}));
105-
const enumType = tsEnum(enumName, schemaObject.enum as (string | number)[], metadata, {
108+
109+
// enums can contain null values, but dont want to output them
110+
let hasNull = false;
111+
const validSchemaEnums = schemaObject.enum.filter((enumValue) => {
112+
if (enumValue === null) {
113+
hasNull = true;
114+
return false;
115+
}
116+
117+
return true;
118+
});
119+
const enumType = tsEnum(enumName, validSchemaEnums as (string | number)[], metadata, {
106120
shouldCache: options.ctx.dedupeEnums,
107121
export: true,
108122
// readonly: TS enum do not support the readonly modifier
109123
});
110124
if (!options.ctx.injectFooter.includes(enumType)) {
111125
options.ctx.injectFooter.push(enumType);
112126
}
113-
return ts.factory.createTypeReferenceNode(enumType.name);
127+
const ref = ts.factory.createTypeReferenceNode(enumType.name);
128+
return hasNull ? tsUnion([ref, NULL]) : ref;
114129
}
115130
const enumType = schemaObject.enum.map(tsLiteral);
116131
if (

packages/openapi-typescript/test/transform/schema-object/string.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ describe("transformSchemaObject > string", () => {
8484
want: '"A" | "B" | "C" | null',
8585
},
8686
],
87+
[
88+
"enum + nullable + null value",
89+
{
90+
given: { type: ["string", "null"], enum: ["A", "B", "C", null] },
91+
want: '"A" | "B" | "C" | null',
92+
},
93+
],
8794
[
8895
"enum + nullable (deprecated syntax)",
8996
{

0 commit comments

Comments
 (0)