Skip to content

Commit

Permalink
[Typescript] update addImport method (fix oneOf) (#11689)
Browse files Browse the repository at this point in the history
* fix addImport method

* add function splitComposedType

* super.addImport

* add docs for splitComposedType

* add docs for splitComposedType
  • Loading branch information
ksvirkou-hubspot committed May 30, 2022
1 parent 6f32206 commit 129fd0a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5230,13 +5230,13 @@ protected void addImport(CodegenModel m, String type) {
addImport(m.imports, type);
}

private void addImport(Set<String> importsToBeAddedTo, String type) {
protected void addImport(Set<String> importsToBeAddedTo, String type) {
if (shouldAddImport(type)) {
importsToBeAddedTo.add(type);
}
}

private boolean shouldAddImport(String type) {
protected boolean shouldAddImport(String type) {
return type != null && needToImport(type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1568,14 +1568,37 @@ protected void addImport(CodegenModel m, String type) {
return;
}

String[] parts = type.split("( [|&] )|[<>]");
String[] parts = splitComposedType(type);
for (String s : parts) {
if (needToImport(s)) {
m.imports.add(s);
}
}
}

@Override
protected void addImport(Set<String> importsToBeAddedTo, String type) {
if (type == null) {
return;
}

String[] parts = splitComposedType(type);
for (String s : parts) {
super.addImport(importsToBeAddedTo, s);
}
}

/**
* Split composed types
* e.g. TheFirstType | TheSecondType to TheFirstType and TheSecondType
*
* @param type String with composed types
* @return list of types
*/
protected String[] splitComposedType(String type) {
return type.replace(" ","").split("[|&<>]");
}

@Override
public GeneratorLanguage generatorLanguage() { return GeneratorLanguage.TYPESCRIPT; }
}

0 comments on commit 129fd0a

Please sign in to comment.