Skip to content

Enum generation descriptions - allow for nulls/empty strings #2393

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 5 commits into from
Aug 11, 2025
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/fluffy-pumas-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Now checking for null & empty string when generating enum description
14 changes: 5 additions & 9 deletions packages/openapi-typescript/src/lib/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export const enumCache = new Map<string, ts.EnumDeclaration>();
export function tsEnum(
name: string,
members: (string | number)[],
metadata?: { name?: string; description?: string }[],
metadata?: { name?: string; description?: string | null }[],
options?: { export?: boolean; shouldCache?: boolean },
) {
let enumName = sanitizeMemberName(name);
Expand Down Expand Up @@ -352,7 +352,7 @@ function sanitizeMemberName(name: string) {
}

/** Sanitize TS enum member expression */
export function tsEnumMember(value: string | number, metadata: { name?: string; description?: string } = {}) {
export function tsEnumMember(value: string | number, metadata: { name?: string; description?: string | null } = {}) {
let name = metadata.name ?? String(value);
if (!JS_PROPERTY_INDEX_RE.test(name)) {
if (Number(name[0]) >= 0) {
Expand Down Expand Up @@ -388,16 +388,12 @@ export function tsEnumMember(value: string | number, metadata: { name?: string;
member = ts.factory.createEnumMember(name, ts.factory.createStringLiteral(value));
}

if (metadata.description === undefined) {
const trimmedDescription = metadata.description?.trim();
if (trimmedDescription === undefined || trimmedDescription === null || trimmedDescription === "") {
return member;
}

return ts.addSyntheticLeadingComment(
member,
ts.SyntaxKind.SingleLineCommentTrivia,
" ".concat(metadata.description.trim()),
true,
);
return ts.addSyntheticLeadingComment(member, ts.SyntaxKind.SingleLineCommentTrivia, ` ${trimmedDescription}`, true);
}

/** Create an intersection type */
Expand Down
6 changes: 5 additions & 1 deletion packages/openapi-typescript/test/lib/ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,11 @@ describe("tsEnum", () => {
tsEnum(
".Error.code.",
[100, 101, 102],
[{ name: "Unauthorized", description: "User is unauthorized" }, { name: "NotFound" }],
[
{ name: "Unauthorized", description: "User is unauthorized" },
{ name: "NotFound", description: "" },
{ name: "Value102", description: null },
],
),
).trim(),
).toBe(`enum ErrorCode {
Expand Down
Loading