forked from loafle/openapi-generator-original
`toModelImportMap` only accounts for union types and not intersection types, so I've combined those cases into `splitComposedType`. For `splitComposedType` I combined `replace(" ","").split("\\|")` and `split("( [|&] )|[<>]")` into `replace(" ","").split("[|&<>]")`. I've added the `needToImport` check to `toModelImportMap`. This is the same check that happens in `addImport`.
This commit is contained in:
parent
a8c4cbd14d
commit
c5fefa938a
@ -227,11 +227,15 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelImport( String name){
|
||||
public String toModelImport(String name){
|
||||
if(isUnionType(name)){
|
||||
LOGGER.warn("The import is a union type. Consider using the toModelImportMap method.");
|
||||
return toModelImportMap(name).values().stream().collect(Collectors.joining("|"));
|
||||
}
|
||||
if(isIntersectionType(name)){
|
||||
LOGGER.warn("The import is a intersection type. Consider using the toModelImportMap method.");
|
||||
return toModelImportMap(name).values().stream().collect(Collectors.joining("&"));
|
||||
}
|
||||
return super.toModelImport(name);
|
||||
}
|
||||
|
||||
@ -243,26 +247,28 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
* @return Map between the fully qualified model import and the initial given name.
|
||||
*/
|
||||
@Override
|
||||
public Map<String,String> toModelImportMap( String name){
|
||||
if(isUnionType(name)){
|
||||
String[] names = splitUnionType(name);
|
||||
return toImportMap(names);
|
||||
}
|
||||
return toImportMap(name);
|
||||
public Map<String,String> toModelImportMap(String name){
|
||||
return toImportMap(splitComposedType(name));
|
||||
}
|
||||
|
||||
private String[] splitComposedType (String name) {
|
||||
return name.replace(" ","").split("[|&<>]");
|
||||
}
|
||||
|
||||
private boolean isUnionType(String name){
|
||||
return name.contains("|");
|
||||
}
|
||||
|
||||
private String[] splitUnionType(String name){
|
||||
return name.replace(" ","").split("\\|");
|
||||
private boolean isIntersectionType(String name){
|
||||
return name.contains("&");
|
||||
}
|
||||
|
||||
private Map<String,String> toImportMap(String... names){
|
||||
Map<String,String> result = Maps.newHashMap();
|
||||
for(String name: names){
|
||||
result.put(toModelImport(name),name);
|
||||
if(needToImport(name)){
|
||||
result.put(toModelImport(name), name);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -707,7 +713,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
|
||||
return;
|
||||
}
|
||||
|
||||
String[] parts = type.split("( [|&] )|[<>]");
|
||||
String[] parts = splitComposedType(type);
|
||||
for (String s : parts) {
|
||||
if (needToImport(s)) {
|
||||
m.imports.add(s);
|
||||
|
@ -12,6 +12,8 @@ import org.testng.annotations.Test;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class SharedTypeScriptTest {
|
||||
@Test
|
||||
@ -69,4 +71,25 @@ public class SharedTypeScriptTest {
|
||||
|
||||
FileUtils.deleteDirectory(new File("src/test/resources/oldImportsStillPresentTest/"));
|
||||
}
|
||||
|
||||
/*
|
||||
#8000
|
||||
Test that primatives are not returned by toModelImportMap
|
||||
*/
|
||||
@Test
|
||||
public void toModelImportMapTest() {
|
||||
TypeScriptAxiosClientCodegen codegen = new TypeScriptAxiosClientCodegen();
|
||||
|
||||
Map<String, String[]> types = new HashMap<String, String[]>() {{
|
||||
put("Schema & AnotherSchema", new String[]{ "Schema", "AnotherSchema" });
|
||||
put("Schema | AnotherSchema", new String[]{ "Schema", "AnotherSchema" });
|
||||
put("Schema & object", new String[]{ "Schema" });
|
||||
put("Schema | object", new String[]{ "Schema" });
|
||||
}};
|
||||
|
||||
for (Map.Entry<String, String[]> entry : types.entrySet()) {
|
||||
String[] mapped = codegen.toModelImportMap(entry.getKey()).values().toArray(new String[0]);
|
||||
Assert.assertEquals(mapped, entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user