Skip to content

Commit 76c1c19

Browse files
committed
Merge branch 'fix/typescript-as-const-trailing-commas' of https://github.com/scarf005/openapi-generator into scarf005-fix/typescript-as-const-trailing-commas
2 parents 4a0d560 + d9cb3b1 commit 76c1c19

278 files changed

Lines changed: 414 additions & 126 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/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
@@ -45,10 +45,10 @@ export enum {{classname}}{{enumName}} {
4545
export const {{classname}}{{enumName}} = {
4646
{{#allowableValues}}
4747
{{#enumVars}}
48-
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
48+
{{{name}}}: {{{value}}},
4949
{{/enumVars}}
5050
{{/allowableValues}}
5151
} as const;
5252
export type {{classname}}{{enumName}} = typeof {{classname}}{{enumName}}[keyof typeof {{classname}}{{enumName}}];
5353
{{/stringEnums}}
54-
{{/isEnum}}{{/vars}}{{/hasEnums}}{{>validationAttributes}}
54+
{{/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/additional-properties-in-multipart-issue/models/StructuredType.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface StructuredType {
2525
someString?: string;
2626
}
2727

28+
2829
/**
2930
* Check if a given object implements the StructuredType interface.
3031
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export interface ExtendDto extends TestBaseDto {
4242

4343

4444

45+
4546
/**
4647
* Check if a given object implements the ExtendDto interface.
4748
*/

0 commit comments

Comments
 (0)