forked from loafle/openapi-generator-original
[bug][typescript] Fix node client generator import file paths (#7410)
This commit is contained in:
parent
620f8db3d6
commit
ca5d384081
@ -36,7 +36,8 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
|
|||||||
private static final Logger LOGGER = LoggerFactory.getLogger(TypeScriptNodeClientCodegen.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(TypeScriptNodeClientCodegen.class);
|
||||||
|
|
||||||
public static final String NPM_REPOSITORY = "npmRepository";
|
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 npmRepository = null;
|
||||||
protected String apiSuffix = "Api";
|
protected String apiSuffix = "Api";
|
||||||
@ -153,7 +154,7 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
|
|||||||
return importMapping.get(name);
|
return importMapping.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return DEFAULT_IMPORT_PREFIX + camelize(toModelName(name), true);
|
return DEFAULT_MODEL_FILENAME_DIRECTORY_PREFIX + camelize(toModelName(name), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -162,7 +163,7 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
|
|||||||
return importMapping.get(name);
|
return importMapping.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return modelPackage() + "/" + camelize(toModelName(name), true);
|
return DEFAULT_MODEL_IMPORT_DIRECTORY_PREFIX + modelPackage() + "/" + camelize(toModelName(name), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -222,8 +223,7 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
|
|||||||
// Add additional filename information for model imports in the apis
|
// Add additional filename information for model imports in the apis
|
||||||
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports");
|
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports");
|
||||||
for (Map<String, Object> im : imports) {
|
for (Map<String, Object> im : imports) {
|
||||||
im.put("filename", im.get("import"));
|
im.put("filename", im.get("import").toString());
|
||||||
im.put("classname", getModelnameFromModelFilename(im.get("filename").toString()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return operations;
|
return operations;
|
||||||
@ -309,10 +309,6 @@ public class TypeScriptNodeClientCodegen extends AbstractTypeScriptClientCodegen
|
|||||||
return toApiFilename(name);
|
return toApiFilename(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getModelnameFromModelFilename(String filename) {
|
|
||||||
String name = filename.substring((modelPackage() + File.separator).length());
|
|
||||||
return camelize(name);
|
|
||||||
}
|
|
||||||
@Override
|
@Override
|
||||||
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
|
protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) {
|
||||||
super.addAdditionPropertiesToCodeGenModel(codegenModel, schema);
|
super.addAdditionPropertiesToCodeGenModel(codegenModel, schema);
|
||||||
|
@ -5,7 +5,7 @@ import http from 'http';
|
|||||||
|
|
||||||
/* tslint:disable:no-unused-locals */
|
/* tslint:disable:no-unused-locals */
|
||||||
{{#imports}}
|
{{#imports}}
|
||||||
import { {{classname}} } from '../{{filename}}';
|
import { {{classname}} } from '{{filename}}';
|
||||||
{{/imports}}
|
{{/imports}}
|
||||||
|
|
||||||
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
|
import { ObjectSerializer, Authentication, VoidAuth, Interceptor } from '../model/models';
|
||||||
|
@ -7,6 +7,8 @@ import org.testng.Assert;
|
|||||||
import org.testng.annotations.BeforeMethod;
|
import org.testng.annotations.BeforeMethod;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
public class TypeScriptNodeClientCodegenTest {
|
public class TypeScriptNodeClientCodegenTest {
|
||||||
|
|
||||||
private TypeScriptNodeClientCodegen codegen;
|
private TypeScriptNodeClientCodegen codegen;
|
||||||
@ -87,9 +89,9 @@ public class TypeScriptNodeClientCodegenTest {
|
|||||||
Assert.assertEquals(codegen.toModelFilename("ApiResponse"), mappedName);
|
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() {
|
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")
|
@Test(description = "use mapped name for model import when provided")
|
||||||
@ -134,4 +136,42 @@ public class TypeScriptNodeClientCodegenTest {
|
|||||||
Assert.assertEquals(codegen.toApiImport("Category"), mappedName);
|
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);
|
||||||
|
}};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user