Skip to content

Commit d9cb3b1

Browse files
committed
fix(typescript): add trailing commas to as const enums (#23275)
Keep enum object diffs smaller across Angular, Fetch, and NestJS server generators.
1 parent 5a1aacc commit d9cb3b1

100 files changed

Lines changed: 201 additions & 137 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modules/openapi-generator/src/main/resources/typescript-angular/modelEnum.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const {{classname}} = {
2222
* {{.}}
2323
*/
2424
{{/enumDescription}}
25-
{{name}}: {{{value}}}{{^-last}},{{/-last}}
25+
{{name}}: {{{value}}},
2626
{{/enumVars}}
2727
{{/allowableValues}}
2828
} as const;

modules/openapi-generator/src/main/resources/typescript-angular/modelGenericEnums.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export enum {{classname}}{{enumName}} {
1818
export const {{enumName}} = {
1919
{{#allowableValues}}
2020
{{#enumVars}}
21-
{{name}}: {{{value}}}{{^-last}},{{/-last}}
21+
{{name}}: {{{value}}},
2222
{{/enumVars}}
2323
{{/allowableValues}}
2424
} as const;
@@ -27,4 +27,4 @@ export enum {{classname}}{{enumName}} {
2727
{{/isEnum}}
2828
{{/vars}}
2929
{{^stringEnums}}}{{/stringEnums}}
30-
{{/hasEnums}}
30+
{{/hasEnums}}

modules/openapi-generator/src/main/resources/typescript-fetch/apis.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ export enum {{operationIdCamelCase}}{{enumName}} {
496496
export const {{operationIdCamelCase}}{{enumName}} = {
497497
{{#allowableValues}}
498498
{{#enumVars}}
499-
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
499+
{{{name}}}: {{{value}}},
500500
{{/enumVars}}
501501
{{/allowableValues}}
502502
} as const;

modules/openapi-generator/src/main/resources/typescript-fetch/modelEnumInterfaces.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ export const {{classname}} = {
2929
* {{enumDescription}}
3030
*/
3131
{{/enumDescription}}
32-
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
32+
{{{name}}}: {{{value}}},
3333
{{/enumVars}}
3434
{{/allowableValues}}
3535
} as const;
3636
export type {{classname}} = typeof {{classname}}[keyof typeof {{classname}}];
37-
{{/stringEnums}}
37+
{{/stringEnums}}

modules/openapi-generator/src/main/resources/typescript-fetch/modelGenericInterfaces.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ export enum {{classname}}{{enumName}} {
4141
export const {{classname}}{{enumName}} = {
4242
{{#allowableValues}}
4343
{{#enumVars}}
44-
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
44+
{{{name}}}: {{{value}}},
4545
{{/enumVars}}
4646
{{/allowableValues}}
4747
} as const;
4848
export type {{classname}}{{enumName}} = typeof {{classname}}{{enumName}}[keyof typeof {{classname}}{{enumName}}];
4949
{{/stringEnums}}
50-
{{/isEnum}}{{/vars}}{{/hasEnums}}{{>validationAttributes}}
50+
{{/isEnum}}{{/vars}}{{/hasEnums}}{{>validationAttributes}}

modules/openapi-generator/src/main/resources/typescript-nestjs-server/modelEnum.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const {{classname}} = {
2222
* {{.}}
2323
*/
2424
{{/enumDescription}}
25-
{{name}}: {{{value}}}{{^-last}},{{/-last}}
25+
{{name}}: {{{value}}},
2626
{{/enumVars}}
2727
{{/allowableValues}}
2828
} as const;

modules/openapi-generator/src/main/resources/typescript-nestjs-server/modelGenericEnums.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export namespace {{classname}} {
1818
export const {{enumName}} = {
1919
{{#allowableValues}}
2020
{{#enumVars}}
21-
{{name}}: {{{value}}}{{^-last}},{{/-last}}
21+
{{name}}: {{{value}}},
2222
{{/enumVars}}
2323
{{/allowableValues}}
2424
} as const;
@@ -27,4 +27,4 @@ export namespace {{classname}} {
2727
{{/isEnum}}
2828
{{/vars}}
2929
{{^stringEnums}}}{{/stringEnums}}
30-
{{/hasEnums}}
30+
{{/hasEnums}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/SharedTypeScriptTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.File;
66
import java.io.IOException;
7+
import java.io.UncheckedIOException;
78
import java.nio.charset.StandardCharsets;
89
import java.nio.file.Files;
910
import java.nio.file.Path;
@@ -274,6 +275,32 @@ public void givenMapWithArrayOfEnumsThenCorrectEnumNameIsUsed() throws Exception
274275
}
275276
}
276277

278+
@Test(description = "Issue #23275 - as const enum objects should include trailing commas")
279+
public void generatesTrailingCommasInAsConstEnumObjectsAcrossTypeScriptGenerators() throws Exception {
280+
final String specPath = "src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml";
281+
List<String> generators = Arrays.asList(
282+
"typescript-angular",
283+
"typescript-axios",
284+
"typescript-fetch",
285+
"typescript-nestjs-server"
286+
);
287+
288+
for (String generatorName : generators) {
289+
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
290+
output.deleteOnExit();
291+
292+
CodegenConfigurator configurator = new CodegenConfigurator()
293+
.setGeneratorName(generatorName)
294+
.setInputSpec(specPath)
295+
.setOutputDir(output.getAbsolutePath());
296+
297+
Generator generator = new DefaultGenerator();
298+
generator.opts(configurator.toClientOptInput()).generate();
299+
300+
assertAsConstObjectsUseTrailingCommas(output.toPath(), generatorName);
301+
}
302+
}
303+
277304
@Test(description = "Issue #22748 - Inner enums should not be double-prefixed when model has parent")
278305
public void givenChildModelWithInheritedInnerEnumThenEnumNameIsNotDoublePrefixed() throws Exception {
279306
// This tests that when a child model inherits from a parent that has an inner enum property,
@@ -310,4 +337,41 @@ public void givenChildModelWithInheritedInnerEnumThenEnumNameIsNotDoublePrefixed
310337
"typescript-angular: Should contain 'Employee.ProjectRolesEnum' in " + modelFile);
311338
}
312339

340+
private void assertAsConstObjectsUseTrailingCommas(Path root, String generatorName) throws IOException {
341+
final int[] asConstObjectCount = {0};
342+
343+
try (Stream<Path> paths = Files.walk(root)) {
344+
paths.filter(Files::isRegularFile)
345+
.filter(path -> path.toString().endsWith(".ts"))
346+
.forEach(path -> {
347+
try {
348+
List<String> lines = Files.readAllLines(path);
349+
for (int i = 0; i < lines.size(); i++) {
350+
if (!lines.get(i).strip().equals("} as const;")) {
351+
continue;
352+
}
353+
354+
asConstObjectCount[0]++;
355+
356+
int previousLineIndex = i - 1;
357+
while (previousLineIndex >= 0 && lines.get(previousLineIndex).isBlank()) {
358+
previousLineIndex--;
359+
}
360+
361+
Assert.assertTrue(
362+
previousLineIndex >= 0 && lines.get(previousLineIndex).stripTrailing().endsWith(","),
363+
generatorName + ": Expected trailing comma before '} as const;' in " + path);
364+
}
365+
} catch (IOException e) {
366+
throw new UncheckedIOException(e);
367+
}
368+
});
369+
} catch (UncheckedIOException e) {
370+
throw e.getCause();
371+
}
372+
373+
Assert.assertTrue(asConstObjectCount[0] > 0,
374+
generatorName + ": Expected generated as const enum objects");
375+
}
376+
313377
}

samples/client/others/typescript-angular-v20/builds/query-param-json/model/pageable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface Pageable {
3333
export namespace Pageable {
3434
export const SortDirectionEnum = {
3535
Asc: 'ASC',
36-
Desc: 'DESC'
36+
Desc: 'DESC',
3737
} as const;
3838
export type SortDirectionEnum = typeof SortDirectionEnum[keyof typeof SortDirectionEnum];
3939
}

samples/client/others/typescript-fetch/infinite-recursion-issue/models/TestObjectType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
export const TestObjectType = {
2121
TEST1: 'TEST1',
2222
TEST2: 'TEST2',
23-
unknown_default_open_api: '11184809'
23+
unknown_default_open_api: '11184809',
2424
} as const;
2525
export type TestObjectType = typeof TestObjectType[keyof typeof TestObjectType];
2626

0 commit comments

Comments
 (0)