-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
[Java][jersey2] Fix generated client code for oneOf models if datatype includes arrays #18042
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
Conversation
|
thanks for the PR. the CI tests failed: https://circleci.com/gh/OpenAPITools/openapi-generator/90090 can you please take a look when you've time? |
|
@wing328 I think the CI ought to work now. However, looking through the templates for other Java libraries (esp. Gson), I think I may have found a better way to go about this fix with only adjusting the templates. |
|
@wing328 ok I'm done. Perhaps someone from the Java experts can look over this now. |
bd9e4f4 to
29ed65c
Compare
|
when you've time, can you please PM me via Slack when you've time? I may have other ways to handle array in oneOf/anyOf schema bette.r |
|
Yeah I can do that, if you tell me where and how to find you. I've not used slack for a long while. |
|
please join https://join.slack.com/t/openapi-generator/shared_invite/zt-12jxxd7p2-XUeQM~4pzsU9x~eGLQqX2g and search for "William Cheng" |
29ed65c to
45f67cc
Compare
Changes: * Change jersey2/oneof_model template to use composed schema data * Change adding of imports in AbstractJavaCodegen to use composed schema data * Add escapedDataType property to CodegenProperty so that the data type may be part of identifiers (e.g. in getters) * Update samples
43413ae to
69f4a46
Compare
69f4a46 to
3702fa2
Compare
|
Alright so as per our Slack chat, I've changed the following (and updated the original description where applicable):
Assuming the tests pass, this PR is finally ready to be merged 🥳 |
| return openapiGeneratorIgnoreList; | ||
| } | ||
|
|
||
| @Override |
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 describe what isTypeErasedGenerics does with a line or two and I'll add a docstring to the method below in my upcoming PR?
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.
Sorry for the late reply. isTypeErasedGenerics is meant to tell the code generator whether the target language is erasing the generic parameter(s) at runtime, which has implications for handling the possible types in things like the constructor or instanceof checks. As an example, if the language has type erased generics, the compiler throws an error when you do something like
public Constructor(final List<String> args) {...}
public Constructor(final List<Integer> args) {...}because both the signatures erase to the same signature Constructor(final List args) which is not allowed. Therefore you need a way to check whether the code generation can include the full parametrized type List<String> and do so potentially multiple times, or if it has to only use it once as the base type List.
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.
what about using {{^vendorExtensions.x-duplicated-data-type}} ... {{/vendorExtensions.x-duplicated-data-type}} instead?
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.
Yes I use that as well, but I have to determine on the codegen side whether that is even applicable. In languages that do not erase their generics, List<String> and List<Integer> are different data types, so in those languages using x-duplicated-data-type makes no sense. Depending on whether isTypeErasedGenerics is true, I set the x-duplicated-data-type flag for types affected by type erasure (if there are multiple variants of it).
|
FYI. I've filed #18281 to sync the templates between jersey2 and jersey3 |
|
when you've time, I wonder if you can you also update model_anyof template with the fix 🙏 |
|
I can have a look at it in a few days time I'm sure. |
Description
Changes
As has already been described in #17745, the code generated for Java with Jersey2 for a oneOf-model can be invalid if one of the possible types is of
arraytype. This is presumably due to the fact that #13897 only fixed the templates for okhttp-gson, but not other serialization libraries. This PR updates theoneOf_modeltemplate for jersey2 as well as add a sample which generates a mixed oneOf model using jersey2 so as to catch regressions on this in future.Beyond that, I've added a mustache lambda to
AbstractJavaCodegenwhich replaces angle brackets, spaces and commas with nothing. This is done so that the data type of any parametrized type may be used in identifiers (e.g. method names of getters) to prevent clashing method signatures. This example used to fail compilation due to clashing erasures:whereas now, one can generate something like:
This also has the added benefit of automatically changing the method name should the return type ever change, forcing a user to think about handling the changed datatype, which would be impossible if it were a simple
public List<?> getList().Another benefit is the ability to use the actual generic type that the user can expect instead of just giving them a wildcarded List.
Possible future work
PR checklist
Commit all changed files.
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/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)@bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608 @martin-mfg