fix(typescript): add trailing commas to as const enums - #24693
Conversation
Keep enum object diffs smaller across Angular, Fetch, and NestJS server generators.
…hub.com/scarf005/openapi-generator into scarf005-fix/typescript-as-const-trailing-commas
There was a problem hiding this comment.
1 issue found across 121 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/SharedTypeScriptTest.java">
<violation number="1" location="modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/SharedTypeScriptTest.java:361">
P3: The helper asserts that every `} as const;` literal in every generated `.ts` file is preceded by a comma-terminated member line, not just the enum objects this PR targets. For the four generators only enum templates emit `} as const;` today, so the current spec passes, but the check produces false failures for an enum with zero members (the line before `} as const;` is then the opening `= {` line, which has no comma) or for any future non-enum as-const map emitted without a trailing comma. Scope the assertion to the enum object being checked instead of every as-const literal, or skip objects whose previous line is the opening brace.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| previousLineIndex--; | ||
| } | ||
|
|
||
| Assert.assertTrue( |
There was a problem hiding this comment.
P3: The helper asserts that every } as const; literal in every generated .ts file is preceded by a comma-terminated member line, not just the enum objects this PR targets. For the four generators only enum templates emit } as const; today, so the current spec passes, but the check produces false failures for an enum with zero members (the line before } as const; is then the opening = { line, which has no comma) or for any future non-enum as-const map emitted without a trailing comma. Scope the assertion to the enum object being checked instead of every as-const literal, or skip objects whose previous line is the opening brace.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/SharedTypeScriptTest.java, line 361:
<comment>The helper asserts that every `} as const;` literal in every generated `.ts` file is preceded by a comma-terminated member line, not just the enum objects this PR targets. For the four generators only enum templates emit `} as const;` today, so the current spec passes, but the check produces false failures for an enum with zero members (the line before `} as const;` is then the opening `= {` line, which has no comma) or for any future non-enum as-const map emitted without a trailing comma. Scope the assertion to the enum object being checked instead of every as-const literal, or skip objects whose previous line is the opening brace.</comment>
<file context>
@@ -310,4 +337,41 @@ public void givenChildModelWithInheritedInnerEnumThenEnumNameIsNotDoublePrefixed
+ previousLineIndex--;
+ }
+
+ Assert.assertTrue(
+ previousLineIndex >= 0 && lines.get(previousLineIndex).stripTrailing().endsWith(","),
+ generatorName + ": Expected trailing comma before '} as const;' in " + path);
</file context>
based on #23277 with removed EOF and updated samples.
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
Summary by cubic
Add trailing commas to generated TypeScript enum objects and operation enums to reduce diff noise and align with common formatters. Previously the last member had no trailing comma; now every member ends with a comma, so the line before "} as const;" always has one. Formatting-only; no runtime or API changes.
typescript-angular,typescript-fetch(bothexport enumandas constmaps), andtypescript-nestjs-server; no template change fortypescript-axios. Regenerate samples.} as const;ends with a comma acrosstypescript-angular,typescript-axios,typescript-fetch, andtypescript-nestjs-server.Written for commit f220bee. Summary will update on new commits.