-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Union Types Import Issue #6789
Conversation
@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) |
...erator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java
Show resolved
Hide resolved
...erator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java
Outdated
Show resolved
Hide resolved
return objsUnderProcess; | ||
} | ||
|
||
protected List<Object> splitUniontypeImports(List<Map<String,String>> imports){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks quite similar to the existing logic in TypeScriptAngularClientCodegen (see postProcessAllModels
and parseImports
)
Maybe we should lift the logic from AngularClient to AbstractTypescriptClient instead of pretty much repeating it?
The root problem, as I see it, is that toModelImport
takes a string as an argument rather than a model and returns a string instead of a list. It makes it very limited in terms of handling composite models. But changing that would require a change in Generator and CodegenConfig interfaces. And generally means more work, of course.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally agree on your analysis regarding the signature of the toModelImport(). I did not want to change that. I have also missed the implementation already present in the AngularClient. I will try to move everything up to the parent class.
@FrankEssenberger thanks for your contribution! |
I adjusted my approach and created a second method This seemed much cleaner then to fiddle around with the imports in a post processing step. |
I would like to run the formatter so that the diff looks better. Please let me know how to execute this and I am also happy for further feedback. |
cc @OpenAPITools/generator-core-team as the change impacts default codegen, generator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a brief question about this PR.
modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java
Outdated
Show resolved
Hide resolved
@FrankEssenberger could you please merge the current master and re-generate the samples? |
I have done a |
@FrankEssenberger did you
|
yes this is what I did. If I do this on the not forked repo I do not get the changes in the samples. Also the changes are reordering of dependencies for example:
changes to
For whatever reason. Is there a formatter task present? |
Could it be your IDE that performs code cleanup when committing? |
ah, I just realized that with me map introduction I changed the order of imports. Most likely the hashmap has not a alphabetic ordering when I loop over the keys. I will fix this that the imports are order alphabetically. |
return result; | ||
} | ||
return Collections.singletonMap(toModelImport(name),name); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two methods (toModelImportMap and toModelImport) look very similar (split by |
, remove spaces, process each part). Should one method just delegate to the other? E.g. toModelImport
could call toModelImportMap
and then collect the resulting map back into a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure good point. I introduced small helper methods with a more descriptive name and used the map method inside the single entry one.
The pipeline seems a bit flaky. I see in the ci build the error:
|
And for the travis-ci I see the errorr:
However, if I run the test locally it runs through so perhaps the timeout is reached for some strange reason in the pipeline. |
@FrankEssenberger I re-started the CI builds. |
Cool, your restart helped. You have the magic green finger :-). |
cc @OpenAPITools/generator-core-team for final approval, as the change impacts default codegen, generator |
merged master. Do you need something from my side? |
@FrankEssenberger we need to wait for a final review a a member of the @OpenAPITools/generator-core-team |
@@ -1115,6 +1099,40 @@ private static String generateParameterId(Parameter parameter) { | |||
return operations; | |||
} | |||
|
|||
private Map<String,String> getAllImportsMapppings(Set<String> allImports){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add docstrings to explain what these new (private) functions do (e.g. avoid duplicates)?
We want to document all methods in the default codegen/generator class to make code easier to understand for new contributors. (I do not expect you to document all methods missing the docstring. The community can help with separate PRs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some doc and used a Set to better represent that the entries are unique for the other method.
@wing328 can we merge this? |
|
Yes, I put the change in the |
@FrankEssenberger thanks again for your contribution. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for the PR.
* master: (720 commits) [docs] Update README badges (#7276) Update apiInvoker.mustache and sample file for akka-scala client for issue #7258 fix (#7259) [Dart] Get all enum values in a list (#7166) Update .gitattributes [ci] Set ubuntu workflow verification to autoclrf=true, safeclrf=false Update check-supported-versions.yaml [ci] Update gitattributes and allow skipping docs generation for Windows CI workflows (#7273) [core][bug] FILES is now path relative with no prefixes (#7271) Update check-supported-versions.yaml Update check-supported-versions.yaml (#7268) [Java][jersey2] Add jersey injection dependencies (#7240) [C][Clang Static Analyzer] Remove the useless variable when assembling URL (#7255) Date format dart (#6389) minor enhancement to java client generator (#7253) typescript: Fix Union Types Import Issue (#6789) Modifying the es5 and es6 templates for javascript to handle default values (#6649) [python-exp] simplify examples (#7157) Support for KumuluzEE microprofile runtime (#5944) [C#][netcore] minor improvements and bug fixes (#7244) Deprecate Flash (ActionScript) client generator (#7231) ...
* master: (219 commits) [java] Appropriate instantiation of model with dynamic properties (OpenAPITools#6052) [docs] Update README badges (OpenAPITools#7276) Update apiInvoker.mustache and sample file for akka-scala client for issue OpenAPITools#7258 fix (OpenAPITools#7259) [Dart] Get all enum values in a list (OpenAPITools#7166) Update .gitattributes [ci] Set ubuntu workflow verification to autoclrf=true, safeclrf=false Update check-supported-versions.yaml [ci] Update gitattributes and allow skipping docs generation for Windows CI workflows (OpenAPITools#7273) [core][bug] FILES is now path relative with no prefixes (OpenAPITools#7271) Update check-supported-versions.yaml Update check-supported-versions.yaml (OpenAPITools#7268) [Java][jersey2] Add jersey injection dependencies (OpenAPITools#7240) [C][Clang Static Analyzer] Remove the useless variable when assembling URL (OpenAPITools#7255) Date format dart (OpenAPITools#6389) minor enhancement to java client generator (OpenAPITools#7253) typescript: Fix Union Types Import Issue (OpenAPITools#6789) Modifying the es5 and es6 templates for javascript to handle default values (OpenAPITools#6649) [python-exp] simplify examples (OpenAPITools#7157) Support for KumuluzEE microprofile runtime (OpenAPITools#5944) [C#][netcore] minor improvements and bug fixes (OpenAPITools#7244) ...
This is an attempt to fix the bug reported here: #5467
The problem was that the union types in typescrit e.g.
TypeA | TypeB
represented by theoneOf
keyword in the service definition lead to not valid code:you can either use a comma in the import or split into separate imports. I did the latter one because I thought this could be more solid for models in different files and paths potentially. If they are not all exported from the same path.
I adjusted it for three typescript generators:
Then I stopped and thought it is a good point to start the discussion before I adjust it for all the other Typescript generators.
Since I am new I would highly appreciate some help regarding the following issues:
mvn formatter:format
I get hundreds of code changes.mvn test
fine?PR checklist
If contributing template-only or documentation-only changes which will change sample output, build the project beforehand../bin/generate-samples.sh
to update all Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example./bin/generate-samples.sh bin/config/java*
. For Windows users, please run the script in Git BASH.master