[typescript-angular] Incorrect OperationId Generated (starting with number) (#2130)

append _ at the beginning, as reserved keyword
This commit is contained in:
karismann 2019-02-12 19:26:45 +01:00 committed by William Cheng
parent add63cb981
commit de33360883
3 changed files with 51 additions and 3 deletions

View File

@ -407,9 +407,9 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
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));
}

View File

@ -3,6 +3,13 @@ package org.openapitools.codegen.typescript.typescriptangular;
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 class TypeScriptAngularClientCodegenTest {
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");
}
}

View File

@ -214,4 +214,31 @@ public class TypeScriptAngularModelTest {
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);
}
}