From 60f988a8fd1cf44fc04f3f42ea1f8060688ac28a Mon Sep 17 00:00:00 2001 From: greymoth-jp <246701683+greymoth-jp@users.noreply.github.com> Date: Mon, 29 Jun 2026 21:24:45 +0900 Subject: [PATCH] fix: collapse line breaks in x-enum-descriptions so generated enums stay valid --- .changeset/enum-description-line-break.md | 5 +++++ packages/openapi-typescript/src/lib/ts.ts | 7 ++++++- packages/openapi-typescript/test/lib/ts.test.ts | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .changeset/enum-description-line-break.md diff --git a/.changeset/enum-description-line-break.md b/.changeset/enum-description-line-break.md new file mode 100644 index 000000000..6a7d0199e --- /dev/null +++ b/.changeset/enum-description-line-break.md @@ -0,0 +1,5 @@ +--- +"openapi-typescript": patch +--- + +Keep multi-line `x-enum-descriptions` on a single comment line. A description containing a line break previously ended the `//` comment early and pushed the remaining text onto its own line as code, producing an invalid `enum` (only relevant with the `enum` option). diff --git a/packages/openapi-typescript/src/lib/ts.ts b/packages/openapi-typescript/src/lib/ts.ts index d1f41eb88..c3fef9d9f 100644 --- a/packages/openapi-typescript/src/lib/ts.ts +++ b/packages/openapi-typescript/src/lib/ts.ts @@ -446,7 +446,12 @@ export function tsEnumMember(value: string | number, metadata: { name?: string; return member; } - return ts.addSyntheticLeadingComment(member, ts.SyntaxKind.SingleLineCommentTrivia, ` ${trimmedDescription}`, true); + return ts.addSyntheticLeadingComment( + member, + ts.SyntaxKind.SingleLineCommentTrivia, + ` ${trimmedDescription.replace(LB_RE, " ")}`, + true, + ); } /** Create an intersection type */ diff --git a/packages/openapi-typescript/test/lib/ts.test.ts b/packages/openapi-typescript/test/lib/ts.test.ts index 79e5b4571..4e6731d30 100644 --- a/packages/openapi-typescript/test/lib/ts.test.ts +++ b/packages/openapi-typescript/test/lib/ts.test.ts @@ -324,6 +324,23 @@ describe("tsEnum", () => { Etc_GMTPlus0 = "Etc/GMT+0", Etc_GMTPlus1 = "Etc/GMT+1", Etc_GMT_1 = "Etc/GMT-1" +}`); + }); + + test("multi-line x-enum-descriptions stay on a single comment line", () => { + expect( + astToString( + tsEnum( + ".Error.code.", + [100, 101], + [{ description: "Code 100\nspanning two lines" }, { description: "Code 101" }], + ), + ).trim(), + ).toBe(`enum ErrorCode { + // Code 100 spanning two lines + Value100 = 100, + // Code 101 + Value101 = 101 }`); }); });