diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java index 69a5a6240a22..03fd19bd3b9d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java @@ -942,9 +942,13 @@ public String toEnumVarName(String name, String datatype) { } // string + // Handle + and - signs to prevent duplicate enum keys (e.g., "Etc/GMT+12" vs "Etc/GMT-12") + // Replace them with descriptive names before sanitization to avoid collisions + varName = varName.replaceAll("\\+", "Plus"); + varName = varName.replaceAll("-", "Minus"); + if (isEnumPropertyNamingReplaceSpecialChar()) { - varName = varName.replaceAll("-", "_minus_"); - varName = varName.replaceAll("\\+", "_plus_"); + // Apply additional special character replacements if the option is enabled varName = varName.replaceAll("_+", "_"); } varName = sanitizeName(varName); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/axios/TypeScriptAxiosEnumCollisionTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/axios/TypeScriptAxiosEnumCollisionTest.java new file mode 100644 index 000000000000..632db013dfb5 --- /dev/null +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/axios/TypeScriptAxiosEnumCollisionTest.java @@ -0,0 +1,40 @@ +package org.openapitools.codegen.typescript.axios; + +import org.openapitools.codegen.languages.TypeScriptAxiosClientCodegen; +import org.openapitools.codegen.typescript.TypeScriptGroups; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertNotEquals; +import static org.testng.Assert.assertEquals; + +@Test(groups = {TypeScriptGroups.TYPESCRIPT, TypeScriptGroups.TYPESCRIPT_AXIOS}) +public class TypeScriptAxiosEnumCollisionTest { + + @Test + public void testTimezoneEnumsDontCollide() { + TypeScriptAxiosClientCodegen codegen = new TypeScriptAxiosClientCodegen(); + codegen.processOpts(); + + // Test the problematic timezone values from issue #22074 + String plusTwelve = codegen.toEnumVarName("Etc/GMT+12", "string"); + String minusTwelve = codegen.toEnumVarName("Etc/GMT-12", "string"); + + // These should produce different enum variable names to avoid TS1117 error + assertNotEquals(plusTwelve, minusTwelve, + "Enum variable names for 'Etc/GMT+12' and 'Etc/GMT-12' should be different to avoid TS1117 error"); + + // Test a few more cases to ensure we handle the pattern correctly + String plusOne = codegen.toEnumVarName("Etc/GMT+1", "string"); + String minusOne = codegen.toEnumVarName("Etc/GMT-1", "string"); + + assertNotEquals(plusOne, minusOne, + "Enum variable names for 'Etc/GMT+1' and 'Etc/GMT-1' should be different"); + + // Verify the expected output format with our fix (using default PascalCase naming) + // The + and - should be converted to descriptive names + assertEquals(plusTwelve, "EtcGmtPlus12", "Etc/GMT+12 should become EtcGmtPlus12"); + assertEquals(minusTwelve, "EtcGmtMinus12", "Etc/GMT-12 should become EtcGmtMinus12"); + assertEquals(plusOne, "EtcGmtPlus1", "Etc/GMT+1 should become EtcGmtPlus1"); + assertEquals(minusOne, "EtcGmtMinus1", "Etc/GMT-1 should become EtcGmtMinus1"); + } +}