Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const {{classname}} = {
* {{.}}
*/
{{/enumDescription}}
{{name}}: {{{value}}}{{^-last}},{{/-last}}
{{name}}: {{{value}}},
{{/enumVars}}
{{/allowableValues}}
} as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export enum {{classname}}{{enumName}} {
export const {{enumName}} = {
{{#allowableValues}}
{{#enumVars}}
{{name}}: {{{value}}}{{^-last}},{{/-last}}
{{name}}: {{{value}}},
{{/enumVars}}
{{/allowableValues}}
} as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export enum {{operationIdCamelCase}}{{enumName}} {
{{#allowableValues}}
{{#enumVars}}
{{{name}}} = {{{value}}}{{^-last}},{{/-last}}
{{{name}}} = {{{value}}},
{{/enumVars}}
{{/allowableValues}}
}
Expand All @@ -20,7 +20,7 @@ export enum {{operationIdCamelCase}}{{enumName}} {
export const {{operationIdCamelCase}}{{enumName}} = {
{{#allowableValues}}
{{#enumVars}}
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
{{{name}}}: {{{value}}},
{{/enumVars}}
{{/allowableValues}}
} as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const {{classname}} = {
* {{enumDescription}}
*/
{{/enumDescription}}
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
{{{name}}}: {{{value}}},
{{/enumVars}}
{{/allowableValues}}
} as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export enum {{classname}}{{enumName}} {
export const {{classname}}{{enumName}} = {
{{#allowableValues}}
{{#enumVars}}
{{{name}}}: {{{value}}}{{^-last}},{{/-last}}
{{{name}}}: {{{value}}},
{{/enumVars}}
{{/allowableValues}}
} as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const {{classname}} = {
* {{.}}
*/
{{/enumDescription}}
{{name}}: {{{value}}}{{^-last}},{{/-last}}
{{name}}: {{{value}}},
{{/enumVars}}
{{/allowableValues}}
} as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export namespace {{classname}} {
export const {{enumName}} = {
{{#allowableValues}}
{{#enumVars}}
{{name}}: {{{value}}}{{^-last}},{{/-last}}
{{name}}: {{{value}}},
{{/enumVars}}
{{/allowableValues}}
} as const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -274,6 +275,32 @@ public void givenMapWithArrayOfEnumsThenCorrectEnumNameIsUsed() throws Exception
}
}

@Test(description = "Issue #23275 - as const enum objects should include trailing commas")
public void generatesTrailingCommasInAsConstEnumObjectsAcrossTypeScriptGenerators() throws Exception {
final String specPath = "src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml";
List<String> generators = Arrays.asList(
"typescript-angular",
"typescript-axios",
"typescript-fetch",
"typescript-nestjs-server"
);

for (String generatorName : generators) {
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
output.deleteOnExit();

CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName(generatorName)
.setInputSpec(specPath)
.setOutputDir(output.getAbsolutePath());

Generator generator = new DefaultGenerator();
generator.opts(configurator.toClientOptInput()).generate();

assertAsConstObjectsUseTrailingCommas(output.toPath(), generatorName);
}
}

@Test(description = "Issue #22748 - Inner enums should not be double-prefixed when model has parent")
public void givenChildModelWithInheritedInnerEnumThenEnumNameIsNotDoublePrefixed() throws Exception {
// This tests that when a child model inherits from a parent that has an inner enum property,
Expand Down Expand Up @@ -310,4 +337,41 @@ public void givenChildModelWithInheritedInnerEnumThenEnumNameIsNotDoublePrefixed
"typescript-angular: Should contain 'Employee.ProjectRolesEnum' in " + modelFile);
}

private void assertAsConstObjectsUseTrailingCommas(Path root, String generatorName) throws IOException {
final int[] asConstObjectCount = {0};

try (Stream<Path> paths = Files.walk(root)) {
paths.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".ts"))
.forEach(path -> {
try {
List<String> lines = Files.readAllLines(path);
for (int i = 0; i < lines.size(); i++) {
if (!lines.get(i).strip().equals("} as const;")) {
continue;
}

asConstObjectCount[0]++;

int previousLineIndex = i - 1;
while (previousLineIndex >= 0 && lines.get(previousLineIndex).isBlank()) {
previousLineIndex--;
}

Assert.assertTrue(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

previousLineIndex >= 0 && lines.get(previousLineIndex).stripTrailing().endsWith(","),
generatorName + ": Expected trailing comma before '} as const;' in " + path);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
} catch (UncheckedIOException e) {
throw e.getCause();
}

Assert.assertTrue(asConstObjectCount[0] > 0,
generatorName + ": Expected generated as const enum objects");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface Pageable {
export namespace Pageable {
export const SortDirectionEnum = {
Asc: 'ASC',
Desc: 'DESC'
Desc: 'DESC',
} as const;
export type SortDirectionEnum = typeof SortDirectionEnum[keyof typeof SortDirectionEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
export const TestObjectType = {
TEST1: 'TEST1',
TEST2: 'TEST2',
unknown_default_open_api: '11184809'
unknown_default_open_api: '11184809',
} as const;
export type TestObjectType = typeof TestObjectType[keyof typeof TestObjectType];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Order {
export const StatusEnum = {
Placed: 'placed',
Approved: 'approved',
Delivered: 'delivered'
Delivered: 'delivered',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace Pet {
export const StatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
Sold: 'sold',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Order {
export const StatusEnum = {
Placed: 'placed',
Approved: 'approved',
SHIPPED: 'delivered'
SHIPPED: 'delivered',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace Pet {
export const StatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
Sold: 'sold',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Order {
export const StatusEnum = {
Placed: 'placed',
Approved: 'approved',
Delivered: 'delivered'
Delivered: 'delivered',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace Pet {
export const StatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
Sold: 'sold',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Order {
export const StatusEnum = {
Placed: 'placed',
Approved: 'approved',
Delivered: 'delivered'
Delivered: 'delivered',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace Pet {
export const StatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
Sold: 'sold',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Order {
export const StatusEnum = {
Placed: 'placed',
Approved: 'approved',
SHIPPED: 'delivered'
SHIPPED: 'delivered',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace Pet {
export const StatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
Sold: 'sold',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Order {
export const StatusEnum = {
Placed: 'placed',
Approved: 'approved',
Delivered: 'delivered'
Delivered: 'delivered',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace Pet {
export const StatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
Sold: 'sold',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Order {
export const StatusEnum = {
Placed: 'placed',
Approved: 'approved',
SHIPPED: 'delivered'
SHIPPED: 'delivered',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace Pet {
export const StatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
Sold: 'sold',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Order {
export const StatusEnum = {
Placed: 'placed',
Approved: 'approved',
Delivered: 'delivered'
Delivered: 'delivered',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace Pet {
export const StatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
Sold: 'sold',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Order {
export const StatusEnum = {
Placed: 'placed',
Approved: 'approved',
SHIPPED: 'delivered'
SHIPPED: 'delivered',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace Pet {
export const StatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
Sold: 'sold',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export namespace Order {
export const StatusEnum = {
Placed: 'placed',
Approved: 'approved',
Delivered: 'delivered'
Delivered: 'delivered',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace Pet {
export const StatusEnum = {
Available: 'available',
Pending: 'pending',
Sold: 'sold'
Sold: 'sold',
} as const;
export type StatusEnum = typeof StatusEnum[keyof typeof StatusEnum];
}
Expand Down
Loading
Loading