Fix model name being a type in TS abstract codegen (#16392)

* fix model name being a type in ts abstract codegen

* add a test

* better code format
This commit is contained in:
William Cheng 2023-08-30 19:24:54 +08:00 committed by GitHub
parent 9cc5513a1c
commit bceae5695e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 7 deletions

View File

@ -512,9 +512,9 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
@Override
public String toParamName(String name) {
// obtain the name from nameMapping directly if provided
if (nameMapping.containsKey(name)) {
return nameMapping.get(name);
// obtain the name from parameterNameMapping directly if provided
if (parameterNameMapping.containsKey(name)) {
return parameterNameMapping.get(name);
}
name = sanitizeName(name, "[^\\w$]");
@ -557,6 +557,11 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
@Override
public String toModelName(final String name) {
// obtain the name from modelNameMapping directly if provided
if (modelNameMapping.containsKey(name)) {
return modelNameMapping.get(name);
}
String fullModelName = name;
fullModelName = addPrefix(fullModelName, modelNamePrefix);
fullModelName = addSuffix(fullModelName, modelNameSuffix);
@ -630,6 +635,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
} else if (ModelUtils.isBinarySchema(p)) {
return "ArrayBuffer";
}
return super.getTypeDeclaration(p);
}
@ -754,6 +760,17 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
@Override
public String getSchemaType(Schema p) {
// check if $ref is a model and modelNameMapping is used
if (StringUtils.isNotBlank(p.get$ref())) {
Schema unaliasSchema = unaliasSchema(p);
Schema actualSchema = ModelUtils.getReferencedSchema(openAPI, unaliasSchema);
String modelName = ModelUtils.getSimpleRef(unaliasSchema.get$ref());
if (ModelUtils.isModel(actualSchema) && modelNameMapping.containsKey(modelName)) {
return toModelName(modelNameMapping.get(modelName));
}
}
String openAPIType = super.getSchemaType(p);
String type = null;
if (ModelUtils.isComposedSchema(p)) {

View File

@ -226,10 +226,10 @@ public class TypeScriptAngularClientCodegenTest {
final String modelName = "FooResponse__links";
final Schema schema = new Schema()
.name(modelName)
.description("an inline model with name previously prefixed with underscore")
.addRequiredItem("self")
.addProperty("self", new StringSchema());
.name(modelName)
.description("an inline model with name previously prefixed with underscore")
.addRequiredItem("self")
.addProperty("self", new StringSchema());
OpenAPI openAPI = TestUtils.createOpenAPIWithOneSchema("test", schema);
codegen.setOpenAPI(openAPI);
@ -313,4 +313,35 @@ public class TypeScriptAngularClientCodegenTest {
"export type Token = ExpressionToken | StringToken"
);
}
@Test
public void testModelNameMappings() throws Exception {
final String specPath = "src/test/resources/2_0/issue_8289.json";
Map<String, Object> properties = new HashMap<>();
properties.put(TypeScriptAngularClientCodegen.TAGGED_UNIONS, "true");
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();
Map<String, String> modelNames = new HashMap<>();
modelNames.put("File", "SystemFile");
final CodegenConfigurator configurator = new CodegenConfigurator()
.setModelNameMappings(modelNames)
.setGeneratorName("typescript-angular")
.setInputSpec(specPath)
.setAdditionalProperties(properties)
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
final ClientOptInput clientOptInput = configurator.toClientOptInput();
Generator generator = new DefaultGenerator();
generator.opts(clientOptInput).generate();
TestUtils.assertFileContains(
Paths.get(output + "/model/folder.ts"),
"files?: Array<SystemFile>;" // ensure it's an array of SystemFile (not Any)
);
}
}

View File

@ -0,0 +1,33 @@
{
"swagger": "2.0",
"info": {
"title": "Test swagger",
"version": "0.1.0"
},
"definitions": {
"File": {
"title": "File",
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
},
"Folder": {
"title": "Folder",
"type": "object",
"properties": {
"files": {
"type": "array",
"items": {
"$ref": "#/definitions/File"
}
}
}
}
},
"paths": {},
"host": "localhost:3000",
"schemes": ["http"]
}