(#8000) Insure toImportMap only returns needToImport (#9275)

`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:
Mike Nelson 2021-06-04 02:03:32 -04:00 committed by GitHub
parent a8c4cbd14d
commit c5fefa938a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 11 deletions

View File

@ -227,11 +227,15 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
} }
@Override @Override
public String toModelImport( String name){ public String toModelImport(String name){
if(isUnionType(name)){ if(isUnionType(name)){
LOGGER.warn("The import is a union type. Consider using the toModelImportMap method."); LOGGER.warn("The import is a union type. Consider using the toModelImportMap method.");
return toModelImportMap(name).values().stream().collect(Collectors.joining("|")); 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); 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. * @return Map between the fully qualified model import and the initial given name.
*/ */
@Override @Override
public Map<String,String> toModelImportMap( String name){ public Map<String,String> toModelImportMap(String name){
if(isUnionType(name)){ return toImportMap(splitComposedType(name));
String[] names = splitUnionType(name); }
return toImportMap(names);
} private String[] splitComposedType (String name) {
return toImportMap(name); return name.replace(" ","").split("[|&<>]");
} }
private boolean isUnionType(String name){ private boolean isUnionType(String name){
return name.contains("|"); return name.contains("|");
} }
private String[] splitUnionType(String name){ private boolean isIntersectionType(String name){
return name.replace(" ","").split("\\|"); return name.contains("&");
} }
private Map<String,String> toImportMap(String... names){ private Map<String,String> toImportMap(String... names){
Map<String,String> result = Maps.newHashMap(); Map<String,String> result = Maps.newHashMap();
for(String name: names){ for(String name: names){
result.put(toModelImport(name),name); if(needToImport(name)){
result.put(toModelImport(name), name);
}
} }
return result; return result;
} }
@ -707,7 +713,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
return; return;
} }
String[] parts = type.split("( [|&] )|[<>]"); String[] parts = splitComposedType(type);
for (String s : parts) { for (String s : parts) {
if (needToImport(s)) { if (needToImport(s)) {
m.imports.add(s); m.imports.add(s);

View File

@ -12,6 +12,8 @@ import org.testng.annotations.Test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.HashMap;
public class SharedTypeScriptTest { public class SharedTypeScriptTest {
@Test @Test
@ -69,4 +71,25 @@ public class SharedTypeScriptTest {
FileUtils.deleteDirectory(new File("src/test/resources/oldImportsStillPresentTest/")); 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());
}
}
} }