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
  • !7410
An error occurred while fetching the assigned milestone of the selected merge_request.

[bug][typescript] Fix node client generator import file paths

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/mahirk/mahirk/fixTypescriptNodeImportModel into master 4 years ago
  • Overview 0
  • Commits 3
  • Pipelines 0
  • Changes 3

Created by: mahirk

Bug and Recap

Issue: Resolves https://github.com/OpenAPITools/openapi-generator/issues/7385

For mapping such as:

  "importMappings": {
    "Pet": "../dataModel/petModel"
  }

Actual

import { AModelPetModel } from '../../dataModel/petModel';

Expected

import { Pet } from '../dataModel/petModel';

For a mapping such as:

  "importMappings": {
    "Pet": "@company/prefix-zoo-store"
  }

Actual

import { NyPrefixZooStore } from '../@company/prefix-zoo-store';

Expected

import { Pet } from '@company/prefix-zoo-store';

Method

Below is the map in operations.get("imports") based on the import location:

Model is created by generator

[{import=model/pet, classname=Pet}]

BYOModel

[{import=@company/prefix-zoo-store, classname=Pet}]

Therefore the logic was updated to a simpler version, assuming the mapping is created by prior areas.

for (Map<String, Object> im : imports) {
     im.put("filename", im.get("import").toString());
}

This resulted in the following import for:

Created Model

import { Pet } from '../model/pet';

BYOModel

import { Pet } from '../@company/prefix-zoo-store';

Per the above, this fixed the issue of the improper render which led to the bad classname i.e. what led to the following NyPrefixZooStore

The next issue was the extra ../ which is embedded as part of the template. See: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache#L8

For this purpose, the extra directory listing was remove from the template and added into the Codegen itself, where the ../ is appended by default ONLY when the model is derived from an auto generated file.

After this change here are the mappings in operations.get("imports")

Model is created by generator

[{import=../model/pet, classname=Pet}]

BYOModel

[{import=@company/prefix-zoo-store, classname=Pet}]

This resulted in the following import for:

Created Model

import { Pet } from '../model/pet';

BYOModel

import { Pet } from '@company/prefix-zoo-store';

Testing

Unit

  1. Updated tests for defaultModelImportTest
  2. Added tests for postProcessOperationsWithModels

Manual

  1. Ran the above expected v/s actual output comparisons to ensure the values provided are accurate

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.
  • If contributing template-only or documentation-only changes which will change sample output, build the project beforehand.
  • Run the shell script ./bin/generate-samples.shto update all Petstore samples related to your fix. 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
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.
Compare
  • master (base)

and
  • latest version
    f112e674
    3 commits, 2 years ago

3 files
+ 48
- 12

    Preferences

    File browser
    Compare changes
modules/openap‎i-generator/src‎
ma‎in‎
java/org/openapitoo‎ls/codegen/languages‎
TypeScriptNodeCl‎ientCodegen.java‎ +5 -9
resources/ty‎pescript-node‎
api-singl‎e.mustache‎ +1 -1
test/java/…/…/…/‎…/typescriptnode‎
TypeScriptNodeClie‎ntCodegenTest.java‎ +42 -2
modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptNodeClientCodegen.java
+ 5
- 9
  • View file @ f112e674

  • Edit in single-file editor

  • Open in Web IDE


@@ -36,7 +36,8 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
private static final Logger LOGGER = LoggerFactory.getLogger(TypeScriptNodeClientCodegen.class);
public static final String NPM_REPOSITORY = "npmRepository";
private static final String DEFAULT_IMPORT_PREFIX = "./";
private static final String DEFAULT_MODEL_FILENAME_DIRECTORY_PREFIX = "./";
private static final String DEFAULT_MODEL_IMPORT_DIRECTORY_PREFIX = "../";
protected String npmRepository = null;
protected String apiSuffix = "Api";
@@ -153,7 +154,7 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
return importMapping.get(name);
}
return DEFAULT_IMPORT_PREFIX + camelize(toModelName(name), true);
return DEFAULT_MODEL_FILENAME_DIRECTORY_PREFIX + camelize(toModelName(name), true);
}
@Override
@@ -162,7 +163,7 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
return importMapping.get(name);
}
return modelPackage() + "/" + camelize(toModelName(name), true);
return DEFAULT_MODEL_IMPORT_DIRECTORY_PREFIX + modelPackage() + "/" + camelize(toModelName(name), true);
}
@Override
@@ -222,8 +223,7 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
// Add additional filename information for model imports in the apis
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports");
for (Map<String, Object> im : imports) {
im.put("filename", im.get("import"));
im.put("classname", getModelnameFromModelFilename(im.get("filename").toString()));
im.put("filename", im.get("import").toString());
}
return operations;
@@ -309,10 +309,6 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
return toApiFilename(name);
}
private String getModelnameFromModelFilename(String filename) {
String name = filename.substring((modelPackage() + File.separator).length());
return camelize(name);
}
@Override
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
super.addAdditionPropertiesToCodeGenModel(codegenModel, schema);
modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache
+ 1
- 1
  • View file @ f112e674

  • Edit in single-file editor

  • Open in Web IDE


@@ -5,7 +5,7 @@ import http from 'http';
/* tslint:disable:no-unused-locals */
{{#imports}}
import { {{classname}} } from '../{{filename}}';
import { {{classname}} } from '{{filename}}';
{{/imports}}
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/typescriptnode/TypeScriptNodeClientCodegenTest.java
+ 42
- 2
  • View file @ f112e674

  • Edit in single-file editor

  • Open in Web IDE


@@ -7,6 +7,8 @@ import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.*;
public class TypeScriptNodeClientCodegenTest {
private TypeScriptNodeClientCodegen codegen;
@@ -87,9 +89,9 @@ public class TypeScriptNodeClientCodegenTest {
Assert.assertEquals(codegen.toModelFilename("ApiResponse"), mappedName);
}
@Test(description = "prepend model import with ./ by default")
@Test(description = "prepend model import with ../model by default")
public void defaultModelImportTest() {
Assert.assertEquals(codegen.toModelImport("ApiResponse"), "model/apiResponse");
Assert.assertEquals(codegen.toModelImport("ApiResponse"), "../model/apiResponse");
}
@Test(description = "use mapped name for model import when provided")
@@ -134,4 +136,42 @@ public class TypeScriptNodeClientCodegenTest {
Assert.assertEquals(codegen.toApiImport("Category"), mappedName);
}
@Test(description = "correctly produces imports without import mapping")
public void postProcessOperationsWithModelsTestWithoutImportMapping() {
final String importName = "../model/pet";
Map<String, Object> operations = createPostProcessOperationsMapWithImportName(importName);
codegen.postProcessOperationsWithModels(operations, Collections.emptyList());
List<Map<String, Object>> extractedImports = (List<Map<String, Object>>) operations.get("imports");
Assert.assertEquals(extractedImports.get(0).get("filename"), importName);
}
@Test(description = "correctly produces imports with import mapping")
public void postProcessOperationsWithModelsTestWithImportMapping() {
final String importName = "@namespace/dir/category";
Map<String, Object> operations = createPostProcessOperationsMapWithImportName(importName);
codegen.postProcessOperationsWithModels(operations, Collections.emptyList());
List<Map<String, Object>> extractedImports = (List<Map<String, Object>>) operations.get("imports");
Assert.assertEquals(extractedImports.get(0).get("filename"), importName);
}
private Map<String, Object> createPostProcessOperationsMapWithImportName(String importName) {
Map<String, Object> operations = new HashMap<String, Object>() {{
put("operation", Collections.emptyList());
put("classname", "Pet");
}};
Map<String, Object> importList = new HashMap<String, Object>() {{
put("import", importName);
put("classname", "Pet");
}};
List<Map<String, Object>> imports = new ArrayList<>();
imports.add(importList);
return new HashMap<String, Object>() {{
put("operations", operations);
put("imports", imports);
}};
}
}
0 Assignees
None
Assign to
0 Reviewers
None
Request review from
Labels
0
None
0
None
    Assign labels
  • Manage project labels

Milestone
No milestone
None
None
Time tracking
No estimate or time spent
Lock merge request
Unlocked
0
0 participants
Reference:
Source branch: github/fork/mahirk/mahirk/fixTypescriptNodeImportModel

Menu

Explore Projects Groups Snippets