Skip to content

Commit 220fed0

Browse files
committed
[typescript] Apply nameMappings and parameterNameMappings before sanitizing
TypeScriptClientCodegen.toVarName and toParamName sanitized the name before calling the parent method, so the mapping lookup in AbstractTypeScriptClientCodegen saw the sanitized name ("id" instead of "$id") and never matched. Look the original name up first, as the parent class does.
1 parent e8687a8 commit 220fed0

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ private String getReturnType(List<CodegenResponse> responses) {
269269

270270
@Override
271271
public String toParamName(String name) {
272+
// obtain the name from parameterNameMapping directly if provided (before sanitizing, which would change the key)
273+
if (parameterNameMapping.containsKey(name)) {
274+
return parameterNameMapping.get(name);
275+
}
276+
272277
// sanitize name
273278
name = sanitizeName(name);
274279

@@ -286,6 +291,11 @@ public String toParamName(String name) {
286291

287292
@Override
288293
public String toVarName(String name) {
294+
// obtain the name from nameMapping directly if provided (before sanitizing, which would change the key)
295+
if (nameMapping.containsKey(name)) {
296+
return nameMapping.get(name);
297+
}
298+
289299
// sanitize name
290300
name = sanitizeName(name);
291301

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ public void modelImportWithMappingTest() {
146146
Assert.assertEquals(tsImports.get(0).get("classname"), "ApiResponse");
147147
}
148148

149+
@Test
150+
public void testNameMappingsUseTheOriginalName() {
151+
final TypeScriptClientCodegen codegen = new TypeScriptClientCodegen();
152+
codegen.nameMapping().put("$id", "schemaId");
153+
codegen.parameterNameMapping().put("$filter", "filterExpression");
154+
155+
// sanitizing turns "$id" into "id", so the mapping has to be looked up first
156+
Assert.assertEquals(codegen.toVarName("$id"), "schemaId");
157+
Assert.assertEquals(codegen.toVarName("id"), "id");
158+
Assert.assertEquals(codegen.toParamName("$filter"), "filterExpression");
159+
Assert.assertEquals(codegen.toParamName("filter"), "filter");
160+
}
161+
149162
@Test
150163
public void testCompilePattern() {
151164
final DefaultCodegen codegen = new TypeScriptClientCodegen();

0 commit comments

Comments
 (0)