Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typescript-node] fix handling of --model-name-prefix|suffix options, second attempt #11966

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.File;
import java.util.*;

import static org.apache.commons.lang3.StringUtils.capitalize;
import static org.openapitools.codegen.utils.StringUtils.camelize;

public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen {
Expand Down Expand Up @@ -189,7 +190,7 @@ private List<Map<String, String>> toTsImports(CodegenModel cm, Set<String> impor
if (!im.equals(cm.classname)) {
HashMap<String, String> tsImport = new HashMap<>();
tsImport.put("classname", im);
tsImport.put("filename", toModelFilename(im));
tsImport.put("filename", toModelFilename(removeModelPrefixSuffix(im)));
tsImports.add(tsImport);
}
}
Expand Down Expand Up @@ -309,6 +310,20 @@ private String getApiFilenameFromClassname(String classname) {
return toApiFilename(name);
}

private String removeModelPrefixSuffix(String name) {
String result = name;
final String prefix = capitalize(this.modelNamePrefix);
final String suffix = capitalize(this.modelNameSuffix);

if (prefix.length() > 0 && result.startsWith(prefix)) {
result = result.substring(prefix.length());
}
if (suffix.length() > 0 && result.endsWith(suffix)) {
result = result.substring(0, result.length() - suffix.length());
}
return result;
}

@Override
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
super.addAdditionPropertiesToCodeGenModel(codegenModel, schema);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.openapitools.codegen.typescript.typescriptnode;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.TestUtils;
import org.openapitools.codegen.languages.TypeScriptNodeClientCodegen;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -157,6 +163,64 @@ public void postProcessOperationsWithModelsTestWithImportMapping() {
Assert.assertEquals(extractedImports.get(0).get("filename"), importName);
}

@Test(description = "correctly produces imports with model name suffix")
public void postProcessOperationsWithModelsTestWithModelNameSuffix() {
final OpenAPI openAPI = TestUtils.createOpenAPI();
final Schema rootSchema = new ObjectSchema()
.addProperties("child", new Schema().$ref("Child"));
final Schema childSchema = new ObjectSchema()
.addProperties("key", new StringSchema());

openAPI.getComponents()
.addSchemas("Root", rootSchema)
.addSchemas("Child", childSchema);

final TypeScriptNodeClientCodegen codegen = new TypeScriptNodeClientCodegen();
codegen.setModelNameSuffix("Suffix");

final HashMap<String, ModelsMap> allModels = createParameterForPostProcessAllModels(
codegen.fromModel("Root", rootSchema),
codegen.fromModel("Child", childSchema)
);
final Map<String, ModelsMap> results = codegen.postProcessAllModels(allModels);
final List<ModelMap> rootModelMaps = results.get("Root")
.getModels();
final List<Map<String, String>> tsImports = (List<Map<String, String>>) rootModelMaps.get(0)
.get("tsImports");

Assert.assertEquals(tsImports.size(), 1);
Assert.assertEquals(tsImports.get(0).get("filename"), "./childSuffix");
}

@Test(description = "correctly produces imports with model name prefix")
public void postProcessOperationsWithModelsTestWithModelNamePrefix() {
final OpenAPI openAPI = TestUtils.createOpenAPI();
final Schema rootSchema = new ObjectSchema()
.addProperties("child", new Schema().$ref("Child"));
final Schema childSchema = new ObjectSchema()
.addProperties("key", new StringSchema());

openAPI.getComponents()
.addSchemas("Root", rootSchema)
.addSchemas("Child", childSchema);

final TypeScriptNodeClientCodegen codegen = new TypeScriptNodeClientCodegen();
codegen.setModelNamePrefix("Prefix");

final HashMap<String, ModelsMap> allModels = createParameterForPostProcessAllModels(
codegen.fromModel("Root", rootSchema),
codegen.fromModel("Child", childSchema)
);
final Map<String, ModelsMap> results = codegen.postProcessAllModels(allModels);
final List<ModelMap> rootModelMaps = results.get("Root")
.getModels();
final List<Map<String, String>> tsImports = (List<Map<String, String>>) rootModelMaps.get(0)
.get("tsImports");

Assert.assertEquals(tsImports.size(), 1);
Assert.assertEquals(tsImports.get(0).get("filename"), "./prefixChild");
}

private Map<String, Object> createPostProcessOperationsMapWithImportName(String importName) {
Map<String, Object> operations = new HashMap<String, Object>() {{
put("operation", Collections.emptyList());
Expand All @@ -174,4 +238,22 @@ private Map<String, Object> createPostProcessOperationsMapWithImportName(String
put("imports", imports);
}};
}

private HashMap<String, ModelsMap> createParameterForPostProcessAllModels(CodegenModel root, CodegenModel child) {
final ModelsMap rootModelsMap = new ModelsMap();
final ModelMap rootModelMap = new ModelMap();
rootModelMap.setModel(root);
rootModelsMap.setModels(Collections.singletonList(rootModelMap));
rootModelsMap.setImports(Collections.singletonList(Collections.singletonMap("import", "../model/Child")));

final ModelsMap childModelsMap = new ModelsMap();
final ModelMap childModelMap = new ModelMap();
childModelMap.setModel(child);
childModelsMap.setModels(Collections.singletonList(childModelMap));

return new HashMap<String, ModelsMap>() {{
put("Child", childModelsMap);
put("Root", rootModelsMap);
}};
}
}