From c5fefa938a7c3960d7020855b74cdf75ddc31ac7 Mon Sep 17 00:00:00 2001 From: Mike Nelson Date: Fri, 4 Jun 2021 02:03:32 -0400 Subject: [PATCH] (#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`. --- .../AbstractTypeScriptClientCodegen.java | 28 +++++++++++-------- .../typescript/SharedTypeScriptTest.java | 23 +++++++++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java index 353c05d5a2e..40dfba5e80f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java @@ -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 toModelImportMap( String name){ - if(isUnionType(name)){ - String[] names = splitUnionType(name); - return toImportMap(names); - } - return toImportMap(name); + public Map 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 toImportMap(String... names){ Map 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); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/SharedTypeScriptTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/SharedTypeScriptTest.java index 9062d738689..f4e024142a5 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/SharedTypeScriptTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/SharedTypeScriptTest.java @@ -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 types = new HashMap() {{ + 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 entry : types.entrySet()) { + String[] mapped = codegen.toModelImportMap(entry.getKey()).values().toArray(new String[0]); + Assert.assertEquals(mapped, entry.getValue()); + } + } } \ No newline at end of file