Skip to content

Commit

Permalink
fix: Support for generating enums when enums definition has null value (
Browse files Browse the repository at this point in the history
#1873)

* fix: Support for generating enums when enums definition has null value

* change to patch

---------

Co-authored-by: Daniel DeMicco <daniel.demicco@reddit.com>
  • Loading branch information
DanDeMicco and Daniel DeMicco authored Oct 25, 2024
1 parent 08b537b commit c2c396d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
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

0 comments on commit c2c396d

Please sign in to comment.