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 e6082564364c..f135af4af943 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 @@ -407,9 +407,9 @@ public String toOperationId(String operationId) { throw new RuntimeException("Empty method name (operationId) not allowed"); } - // method name cannot use reserved keyword, e.g. return - // append _ at the beginning, e.g. _return - if (isReservedWord(operationId)) { + // method name cannot use reserved keyword or word starting with number, e.g. return or 123return + // append _ at the beginning, e.g. _return or _123return + if (isReservedWord(operationId) || operationId.matches("^\\d.*")) { return escapeReservedWord(camelize(sanitizeName(operationId), true)); } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java index 00025c5fe512..256dd93b314f 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularClientCodegenTest.java @@ -3,6 +3,13 @@ import org.junit.Assert; import org.junit.Test; import org.openapitools.codegen.languages.TypeScriptAngularClientCodegen; +import org.openapitools.codegen.TestUtils; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.Operation; +import org.openapitools.codegen.CodegenOperation; +import io.swagger.v3.oas.models.PathItem; +import io.swagger.v3.oas.models.responses.ApiResponse; +import io.swagger.v3.oas.models.responses.ApiResponses; public class TypeScriptAngularClientCodegenTest { @Test @@ -14,4 +21,18 @@ public void testModelFileSuffix() { Assert.assertEquals(codegen.toModelFilename("testName"), "testNameMySuffix"); } + + @Test + public void testOperationIdParser() { + OpenAPI openAPI = TestUtils.createOpenAPI(); + Operation operation1 = new Operation().operationId("123_test_@#$%_special_tags").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK"))); + openAPI.path("another-fake/dummy/", new PathItem().get(operation1)); + final TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen(); + codegen.setOpenAPI(openAPI); + + CodegenOperation co1 = codegen.fromOperation("/another-fake/dummy/", "get", operation1, null); + org.testng.Assert.assertEquals(co1.operationId, "_123testSpecialTags"); + + } + } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java index 9187a5674e1a..77f89e537de1 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java @@ -214,4 +214,31 @@ public void mapModelTest() { Assert.assertEquals(cm.additionalPropertiesType, "Children"); Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1); } + + @Test(description = "convert a model with a name starting with decimal") + public void beginDecimalNameTest() { + final Schema schema = new Schema() + .description("a model with a name starting with decimal") + .addProperties("1list", new StringSchema()) + .addRequiredItem("1list"); + final DefaultCodegen codegen = new TypeScriptAngularClientCodegen(); + OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("sample", schema); + codegen.setOpenAPI(openAPI); + final CodegenModel cm = codegen.fromModel("sample", schema); + + Assert.assertEquals(cm.name, "sample"); + Assert.assertEquals(cm.classname, "Sample"); + Assert.assertEquals(cm.vars.size(), 1); + + final CodegenProperty property = cm.vars.get(0); + Assert.assertEquals(property.baseName, "1list"); + Assert.assertEquals(property.dataType, "string"); + Assert.assertEquals(property.name, "_1list"); + Assert.assertEquals(property.defaultValue, "undefined"); + Assert.assertEquals(property.baseType, "string"); + Assert.assertFalse(property.hasMore); + Assert.assertTrue(property.required); + Assert.assertFalse(property.isContainer); + } + }