Skip to content

Commit 8f72be1

Browse files
committed
Implement review comment about the trailing >
This implements comment #22748 (comment)
1 parent 94499fa commit 8f72be1

2 files changed

Lines changed: 47 additions & 13 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,11 +1110,26 @@ protected void updateDataTypeWithEnumForMap(CodegenProperty property) {
11101110
// Now use property.enumName for datatypeWithEnum instead of toEnumName(baseItem)
11111111
// This ensures the correct enum name (e.g., "ProjectRolesEnum") is used
11121112
// instead of the generic inner item name (e.g., "InnerEnum")
1113-
property.datatypeWithEnum = property.datatypeWithEnum.replace(baseItem.baseType + ">", property.enumName + ">");
1113+
//
1114+
// TypeScript map types use index signature syntax: { [key: string]: ValueType; }
1115+
// We must only replace the VALUE type, not the key type (which must remain string/number).
1116+
// Target the value position by replacing "]: baseType" patterns.
1117+
String datatypeWithEnum = property.datatypeWithEnum;
1118+
// Try replacing value type in nested context (e.g., "]: Array<string>>" or "]: string>")
1119+
String replaced = datatypeWithEnum.replace("]: " + baseItem.baseType + ">", "]: " + property.enumName + ">");
1120+
if (replaced.equals(datatypeWithEnum)) {
1121+
// Try replacing value type in simple map context (e.g., "]: string;")
1122+
replaced = datatypeWithEnum.replace("]: " + baseItem.baseType + ";", "]: " + property.enumName + ";");
1123+
}
1124+
if (replaced.equals(datatypeWithEnum)) {
1125+
// Fallback: replace container types like "Array<string>" or "Set<string>"
1126+
replaced = datatypeWithEnum.replace("<" + baseItem.baseType + ">", "<" + property.enumName + ">");
1127+
}
1128+
property.datatypeWithEnum = replaced;
11141129

11151130
// set default value for variable with inner enum
11161131
if (property.defaultValue != null) {
1117-
property.defaultValue = property.defaultValue.replace(", " + property.items.baseType, ", " + property.enumName);
1132+
property.defaultValue = property.defaultValue.replace(baseItem.baseType, property.enumName);
11181133
}
11191134

11201135
updateCodegenPropertyEnum(property);

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,15 @@ public void givenMapWithArrayOfEnumsThenCorrectEnumNameIsUsed() throws Exception
226226
"typescript-rxjs"
227227
);
228228

229-
Pattern modelDefinition = Pattern.compile("\\b(interface|type|class)\\s+EmployeeWithMultiMapOfEnum\\b");
230-
Pattern namespacedEnumRef = Pattern.compile("projectRoles[^\\n]*ProjectRolesEnum");
231-
Pattern flatEnumRef = Pattern.compile("projectRoles[^\\n]*EmployeeWithMultiMapOfEnumProjectRolesEnum");
229+
// Patterns for EmployeeWithMultiMapOfEnum (map of array of enums)
230+
Pattern multiMapModelDefinition = Pattern.compile("\\b(interface|type|class)\\s+EmployeeWithMultiMapOfEnum\\b");
231+
Pattern multiMapNamespacedEnumRef = Pattern.compile("projectRoles[^\\n]*ProjectRolesEnum");
232+
Pattern multiMapFlatEnumRef = Pattern.compile("projectRoles[^\\n]*EmployeeWithMultiMapOfEnumProjectRolesEnum");
233+
234+
// Patterns for EmployeeWithMapOfEnum (simple map of enums)
235+
Pattern simpleMapModelDefinition = Pattern.compile("\\b(interface|type|class)\\s+EmployeeWithMapOfEnum\\b");
236+
Pattern simpleMapNamespacedEnumRef = Pattern.compile("projectRole[^s][^\\n]*ProjectRoleEnum");
237+
Pattern simpleMapFlatEnumRef = Pattern.compile("projectRole[^s][^\\n]*EmployeeWithMapOfEnumProjectRoleEnum");
232238

233239
for (String generatorName : generators) {
234240
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
@@ -242,16 +248,29 @@ public void givenMapWithArrayOfEnumsThenCorrectEnumNameIsUsed() throws Exception
242248
Generator generator = new DefaultGenerator();
243249
generator.opts(configurator.toClientOptInput()).generate();
244250

245-
Path modelFile = findModelDefinitionFile(output.toPath(), modelDefinition);
246-
String fileContents = Files.readString(modelFile);
251+
// Test EmployeeWithMultiMapOfEnum (map of array of enums)
252+
Path multiMapModelFile = findModelDefinitionFile(output.toPath(), multiMapModelDefinition);
253+
String multiMapFileContents = Files.readString(multiMapModelFile);
254+
255+
Assert.assertFalse(multiMapFileContents.contains("InnerEnum"),
256+
generatorName + ": Should not contain 'InnerEnum' reference in " + multiMapModelFile);
257+
258+
boolean hasMultiMapEnumRef = multiMapNamespacedEnumRef.matcher(multiMapFileContents).find()
259+
|| multiMapFlatEnumRef.matcher(multiMapFileContents).find();
260+
Assert.assertTrue(hasMultiMapEnumRef,
261+
generatorName + ": Expected enum reference not found in " + multiMapModelFile);
262+
263+
// Test EmployeeWithMapOfEnum (simple map of enums)
264+
Path simpleMapModelFile = findModelDefinitionFile(output.toPath(), simpleMapModelDefinition);
265+
String simpleMapFileContents = Files.readString(simpleMapModelFile);
247266

248-
Assert.assertFalse(fileContents.contains("InnerEnum"),
249-
generatorName + ": Should not contain 'InnerEnum' reference in " + modelFile);
267+
Assert.assertFalse(simpleMapFileContents.contains("InnerEnum"),
268+
generatorName + ": Should not contain 'InnerEnum' reference in " + simpleMapModelFile);
250269

251-
boolean hasEnumRef = namespacedEnumRef.matcher(fileContents).find()
252-
|| flatEnumRef.matcher(fileContents).find();
253-
Assert.assertTrue(hasEnumRef,
254-
generatorName + ": Expected enum reference not found in " + modelFile);
270+
boolean hasSimpleMapEnumRef = simpleMapNamespacedEnumRef.matcher(simpleMapFileContents).find()
271+
|| simpleMapFlatEnumRef.matcher(simpleMapFileContents).find();
272+
Assert.assertTrue(hasSimpleMapEnumRef,
273+
generatorName + ": Expected enum reference for simple map not found in " + simpleMapModelFile);
255274
}
256275
}
257276

0 commit comments

Comments
 (0)