Skip to content

fix: Support for generating enums when enums definition has null value #1873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/real-penguins-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Support for generating enums when enums definition has null value
21 changes: 18 additions & 3 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,38 @@ export function transformSchemaObjectWithComposition(
!("additionalProperties" in schemaObject)
) {
// hoist enum to top level if string/number enum and option is enabled
if (options.ctx.enum && schemaObject.enum.every((v) => typeof v === "string" || typeof v === "number")) {
if (
options.ctx.enum &&
schemaObject.enum.every((v) => typeof v === "string" || typeof v === "number" || v === null)
) {
let enumName = parseRef(options.path ?? "").pointer.join("/");
// allow #/components/schemas to have simpler names
enumName = enumName.replace("components/schemas", "");
const metadata = schemaObject.enum.map((_, i) => ({
name: schemaObject["x-enum-varnames"]?.[i] ?? schemaObject["x-enumNames"]?.[i],
description: schemaObject["x-enum-descriptions"]?.[i] ?? schemaObject["x-enumDescriptions"]?.[i],
}));
const enumType = tsEnum(enumName, schemaObject.enum as (string | number)[], metadata, {

// enums can contain null values, but dont want to output them
let hasNull = false;
const validSchemaEnums = schemaObject.enum.filter((enumValue) => {
if (enumValue === null) {
hasNull = true;
return false;
}

return true;
});
const enumType = tsEnum(enumName, validSchemaEnums as (string | number)[], metadata, {
shouldCache: options.ctx.dedupeEnums,
export: true,
// readonly: TS enum do not support the readonly modifier
});
if (!options.ctx.injectFooter.includes(enumType)) {
options.ctx.injectFooter.push(enumType);
}
return ts.factory.createTypeReferenceNode(enumType.name);
const ref = ts.factory.createTypeReferenceNode(enumType.name);
return hasNull ? tsUnion([ref, NULL]) : ref;
}
const enumType = schemaObject.enum.map(tsLiteral);
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ describe("transformSchemaObject > string", () => {
want: '"A" | "B" | "C" | null',
},
],
[
"enum + nullable + null value",
{
given: { type: ["string", "null"], enum: ["A", "B", "C", null] },
want: '"A" | "B" | "C" | null',
},
],
[
"enum + nullable (deprecated syntax)",
{
Expand Down