Skip to content

Commit de33360

Browse files
karismannwing328
authored andcommitted
[typescript-angular] Incorrect OperationId Generated (starting with number) (#2130)
append _ at the beginning, as reserved keyword
1 parent add63cb commit de33360

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,9 @@ public String toOperationId(String operationId) {
407407
throw new RuntimeException("Empty method name (operationId) not allowed");
408408
}
409409

410-
// method name cannot use reserved keyword, e.g. return
411-
// append _ at the beginning, e.g. _return
412-
if (isReservedWord(operationId)) {
410+
// method name cannot use reserved keyword or word starting with number, e.g. return or 123return
411+
// append _ at the beginning, e.g. _return or _123return
412+
if (isReservedWord(operationId) || operationId.matches("^\\d.*")) {
413413
return escapeReservedWord(camelize(sanitizeName(operationId), true));
414414
}
415415

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
import org.junit.Assert;
44
import org.junit.Test;
55
import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen;
6+
import org.openapitools.codegen.TestUtils;
7+
import io.swagger.v3.oas.models.OpenAPI;
8+
import io.swagger.v3.oas.models.Operation;
9+
import org.openapitools.codegen.CodegenOperation;
10+
import io.swagger.v3.oas.models.PathItem;
11+
import io.swagger.v3.oas.models.responses.ApiResponse;
12+
import io.swagger.v3.oas.models.responses.ApiResponses;
613

714
public class TypeScriptAngularClientCodegenTest {
815
@Test
@@ -14,4 +21,18 @@ public void testModelFileSuffix() {
1421

1522
Assert.assertEquals(codegen.toModelFilename("testName"), "testNameMySuffix");
1623
}
24+
25+
@Test
26+
public void testOperationIdParser() {
27+
OpenAPI openAPI = TestUtils.createOpenAPI();
28+
Operation operation1 = new Operation().operationId("123_test_@#$%_special_tags").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK")));
29+
openAPI.path("another-fake/dummy/", new PathItem().get(operation1));
30+
final TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen();
31+
codegen.setOpenAPI(openAPI);
32+
33+
CodegenOperation co1 = codegen.fromOperation("/another-fake/dummy/", "get", operation1, null);
34+
org.testng.Assert.assertEquals(co1.operationId, "_123testSpecialTags");
35+
36+
}
37+
1738
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,31 @@ public void mapModelTest() {
214214
Assert.assertEquals(cm.additionalPropertiesType, "Children");
215215
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
216216
}
217+
218+
@Test(description = "convert a model with a name starting with decimal")
219+
public void beginDecimalNameTest() {
220+
final Schema schema = new Schema()
221+
.description("a model with a name starting with decimal")
222+
.addProperties("1list", new StringSchema())
223+
.addRequiredItem("1list");
224+
final DefaultCodegen codegen = new TypeScriptAngularClientCodegen();
225+
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema);
226+
codegen.setOpenAPI(openAPI);
227+
final CodegenModel cm = codegen.fromModel("sample", schema);
228+
229+
Assert.assertEquals(cm.name, "sample");
230+
Assert.assertEquals(cm.classname, "Sample");
231+
Assert.assertEquals(cm.vars.size(), 1);
232+
233+
final CodegenProperty property = cm.vars.get(0);
234+
Assert.assertEquals(property.baseName, "1list");
235+
Assert.assertEquals(property.dataType, "string");
236+
Assert.assertEquals(property.name, "_1list");
237+
Assert.assertEquals(property.defaultValue, "undefined");
238+
Assert.assertEquals(property.baseType, "string");
239+
Assert.assertFalse(property.hasMore);
240+
Assert.assertTrue(property.required);
241+
Assert.assertFalse(property.isContainer);
242+
}
243+
217244
}

0 commit comments

Comments
 (0)