Skip to content
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 @@ -30,6 +30,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,6 +1,10 @@
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.testng.Assert;
Expand Down Expand Up @@ -157,6 +161,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, Object> allModels = createParameterForPostProcessAllModels(
codegen.fromModel("Root", rootSchema),
codegen.fromModel("Child", childSchema)
);
final Map<String, Object> results = codegen.postProcessAllModels(allModels);
final Map<String, Object> root = (Map<String, Object>) results.get("Root");
final List<Map<String, Object>> modelsOfRoot = (List<Map<String, Object>>) root.get("models");
final List<HashMap<String, String>> tsImports = (List<HashMap<String, String>>) modelsOfRoot.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, Object> allModels = createParameterForPostProcessAllModels(
codegen.fromModel("Root", rootSchema),
codegen.fromModel("Child", childSchema)
);
final Map<String, Object> results = codegen.postProcessAllModels(allModels);
final Map<String, Object> root = (Map<String, Object>) results.get("Root");
final List<Map<String, Object>> modelsOfRoot = (List<Map<String, Object>>) root.get("models");
final List<HashMap<String, String>> tsImports = (List<HashMap<String, String>>) modelsOfRoot.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 +236,30 @@ private Map<String, Object> createPostProcessOperationsMapWithImportName(String
put("imports", imports);
}};
}

private HashMap<String, Object> createParameterForPostProcessAllModels(CodegenModel root, CodegenModel child) {
return new HashMap<String, Object>() {{
put("Child", new HashMap<String, Object>() {{
put("models", Collections.singletonList(
new HashMap<String, Object>() {{
put("importPath", "../model/child");
put("model", child);
}}
));
}});
put("Root", new HashMap<String, Object>() {{
put("models", Collections.singletonList(
new HashMap<String, Object>() {{
put("importPath", "../model/root");
put("model", root);
}}
));
put("imports", Collections.singletonList(
new HashMap<String, Object>() {{
put("import", "../model/child");
}}
));
}});
}};
}
}