Skip to content
GitLab
    • Explore Projects Groups Snippets
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !11966

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

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/ty-v1/fix-prefix-suffix-typescript-node into master 3 years ago
  • Overview 0
  • Commits 5
  • Pipelines 0
  • Changes 2

Created by: ty-v1

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*. For Windows users, please run the script in Git BASH.
  • File the PR against the correct branch: master (5.3.0), 6.0.x
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Description of the PR

I added TypeScriptNodeClientCodegen#removeModelPrefixSuffix to remove prefix/suffix defined by --model-name-prefix|suffix options and unit tests.

After merging #11956, the arg type of TypescriptNodeClientCodegen#postProcessAllModels had changed, so I fixed unit tests.

Related issue and PRs

https://github.com/OpenAPITools/openapi-generator/pull/2590 https://github.com/OpenAPITools/openapi-generator/pull/11956 https://github.com/OpenAPITools/openapi-generator/issues/2560

Todo

  • run mvn -e --no-snapshot-updates --quiet --batch-mode --show-version clean install -Dorg.slf4j.simpleLogger.defaultLogLevel=error in local

@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03)

Compare
  • master (base)

and
  • latest version
    12fb6e44
    5 commits, 2 years ago

2 files
+ 98
- 1

    Preferences

    File browser
    Compare changes
modules/openap‎i-generator/src‎
main/java/org/…/‎codegen/languages‎
TypeScriptNodeCl‎ientCodegen.java‎ +16 -1
test/java/…/…/…/‎…/typescriptnode‎
TypeScriptNodeClie‎ntCodegenTest.java‎ +82 -0
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptNodeClientCodegen.java
+ 16
- 1
  • View file @ 12fb6e44

  • Edit in single-file editor

  • Open in Web IDE


@@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory;
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 {
@@ -189,7 +190,7 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
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);
}
}
@@ -309,6 +310,20 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
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);
modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypeScriptNodeClientCodegenTest.java
+ 82
- 0
  • View file @ 12fb6e44

  • Edit in single-file editor

  • Open in Web IDE


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;
@@ -157,6 +163,64 @@ public class TypeScriptNodeClientCodegenTest {
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());
@@ -174,4 +238,22 @@ public class TypeScriptNodeClientCodegenTest {
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);
}};
}
}
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
2
Feature: Generator Issue: Bug
2
Feature: Generator Issue: Bug
    Assign labels
  • Manage project labels

Milestone
4.1.0
4.1.0 (expired)
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference: OpenAPITools/openapi-generator!3498
Source branch: github/fork/ty-v1/fix-prefix-suffix-typescript-node

Menu

Explore Projects Groups Snippets