-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
[REQ][JAVA] Add JsonIgnoreProperties to model #3441
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
base: master
Are you sure you want to change the base?
[REQ][JAVA] Add JsonIgnoreProperties to model #3441
Conversation
…alFieldsAnnotation" to turn off this feature
I ran bin/java-pestore-all.sh and nothing happened. How do I repair the circleci test? |
after running |
…into the jackson block OpenAPITools#3293.
The description of the flag is not really clear. Instead of
I would prefer something as:
I am also not sure about the flag name
To not generate the Otherwise this feature looks interesting… I am wondering if |
See also #3438 (comment) on the issue. For the different jackson configuration, we need to decide (or to offer a way to configure) if we do it with annotations or with proper mapper configuration. Instead of this
At the end, I think that both use-cases are interesting (annotation or mapper) and I do not think that it can be said that one approach is 100% better than the other And we do this consistently for the different Jackson configuration option that we have. For example for "AutoDetect" (described in #3573) we could have:
What do you think? |
I like fine control in my models. If we explicitly declare every object as ignoreUnknown it is easy to change for the developer to specialize this case by removing this annotation, perhaps by adding a x-parameter like "x-forbid-additional-fields" which will in turn remove the annotation to let the client handle that as an bad response instead of silently throwing the error away. Adding it as a default does not destroy anything so I do not see any harm in that. Per default it duplicates the ignore additional fields setting which I would consider as defensive. Removing the setting from the default mapper could lead to some unexpected behaviours if some people are reusing the json mapper for other purposes which is why I left that as it is. About the option to select which variant you want to have: I understand that but would consider it as over-complicating? As an end user I am happy about every setting with which I do not need to bother. I will accept your description and naming suggestion/changes in my next commit. |
Let's say we go with one boolean switch Case 1: If set to
My understanding is that the setting on the mapper is no longer relevant, because the annotation wins. Case 2: The end user of OpenAPI-Generator would like to be notified when unknown properties are present (example scenario: the team develops a back-end and uses OpenAPI Generator to generate a client and is writing integration tests using the generated client), it goes through the docs and finds about the Result: same result because they need to also modify the generated mapper. Not user friendly in my opinion The advantage I see with an option to select the variant: with a single setting in OpenAPI Generator, you tell what you would like to have: not ignoring unknown properties, ignoring them at mapper level (current implementation), ignoring them at model level with annotation, or both (which could become the new default - to not introduce a breaking change in case users are using the mapper as you have mentioned). The complexity comes from the number of options (not following a clear semantic about what they do). With this PR you are adding one. If we want less complexity, let just not add the optinon and always generate the But in this case we remove the flexibilty for people that wants to be notified about unexpected properties in JSON. Right now they just need to exchange a setting in the object-mapper (manageable), if we always generate |
I have another Idea, trying to merge both strategies. We could remove the flag to turn of the annotations and add a new flag which is feature oriented called "ignoreUnknownProperties" which defaults to true. This switch controls both, the object mapper and all annotations. Since generated code is not edited by hand this hopefully should not lead to any problems. If the developer wants to deactivate the ignore feature he/she has to write his/her own mapper at the moment to be able to regenerate the code and update the API. If the variant selection is preferred I would like to change that to an array of "features", that we do not have options like:
but instead a concatenated list of features which could lead to following options: configureJsonParser
I would also only give the option where to configure all json parser configurations, if they should be done in each model directly or in the mapper. Each single option what can be configured or not would be configured with feature-flags like described above. |
I appreciate the effort you do.
I like this approach. Question 1) is there a difference between Question 2) About the name: So this Question 3) Last question: is this new option unique for all configuration option of Jackson (for all feature/annotation) or will there be one per feature:
For the next a patch version (3rd digit of the version), change the generated code as few as possible: I would let it to For the next minor version (2d digit, e.g. |
My opinion is that this kind of configuration should be done globally in the ObjectMapper and not be enforced in the POJO. I think it's already possible to configure the ObjectMapper so I don't think there is anything to do. |
@cbornet: thank you a lot for your feedback.
I respect this opinion, but this is really opinionated. I see @Fjolnir-Dvorak points with having it in the POJO with jackson-annotations. We can not close all PRs coming in with the idea to add the jackson-annotations as won't fix "please do it at Mapper level". This PR is the illustration that the mapper approach does not work for everybody: If we do not support both Mapper and Annotations we will have always PRs like this one and we will end up with contradiction between POJO and mapper. I also think that the set of jackson-features can not be decided by the generator (I plan to add We can come up with the defaults we consider as the best, but some people wants to change those and if we do not provide flags the only option is to maintain custom templates. I do this for our project, but believe me it is hard. |
I agree with @cbornet in general. However, in this case, it seems like a useful thing to allow - use of either annotations or ObjectMapper. The default behavior should be to allow this behavior to be defined in the ObjectMapper as it is now. If different behavior is required, it seems reasonable to allow that customization. Is a parameter the only way to provide this option? (I think so, but just wondering if other options are available or have been considered) |
I have the problem that I have to work with a client where I cannot set or change the jackson parser. In order for the generated code to work I have to assure that it just works without any custom jackson parser. My final goal would be that every configuration is made in the model such that the model is independend from the used parser. That could be done via just removing the configurations from the mapper and adding it to the models on a major release or to add flags. I would like to rethink this whole pull request since I implemented it with the thought of minimal inversive changes:
Some Ideas how this feature can be implemented general:
|
There was a lot of discussion with @OpenAPITools/generator-core-team yesterday. I try to summarize what I understood from the vision:
If implemented this way, this would mean that we have a no-go for this change.
I tend to agree with this. If two flags are influencing something close in the generated code (e.g. both changes the way jackson is configured), they need to have close name and close behavior, so that the user would expect them and would look for them.
The problem with annotation on POJO is not the runtime cost. I can give you my example: I am sharing the models and I reuse them across different use cases:
If you add
I am not sure I get this right. The vision of @cbornet is that the mapper configuration are generated by OpenAPI-Generator, but if someone wants it differently he is free to change the generated code (add to add it to
I would tend to have one flag per jackson feature, otherwise we loose the flexibility [this is my personal opinion]. NOTE: the member of the core-team that discussed this so far are more against new configuration-option.
This does not work for my use case presented above [this is my personal opinion].
This does not work for my use case presented above [this is my personal opinion].
I agree with you that this is the risk. |
No that's not what I think. Sorry if I was unclear and made you feel that. My point is that the generator generates a default mapper that can be overridden. So you can configure a per-SDK mapper however you want. |
And the root issue of @Fjolnir-Dvorak is that he shares the same mapper for all clients and even with other non http-libs (drivers, etc...) which is a design smell as it introduces coupling. |
That is correct. The problem I have currently is ugly and I am trying to fix that (other teams are involved. That does not make it easier) that (hopefully) I will be able to use the provided mapper in the future. If we do not consider the design flaw I have to work with another neat feature would be that all model files would be speaking by themselfs and would be independent from any mapper that the user is not able to use those files in a way which was not intended (at least it is more difficult). I also like the idea of being picky on where to allow additional properties which will be ignored and where not (openapi-property x-jackson-forbid-unknown. Could even be extended to other generators [x-forbid-unknown]) by adding a custom openapi property which toggles the boolean switch from true to false for that specific model which can be useful if the client is using a not yet stable API which is still volatile to prevent malfunctioning. I do not know if this is possible if the mapper is the main configuration (Does an annotation overrides the behaviour of the mapper? Then it would be possible) ignoring for the moment if this feature is wanted or not. Since defaulting to annotations is not possible (use case of jmini) I would let the mapper configuration be the default but would like to give the user the possibility to "bullet-proof" their model as far as possible by moving the configuration into the model. A per feature flag option would be the most versatile but would also have the biggest overhead in the template files and could possibly add a lot of flags. Also it has to be implemented for every feature separately which does make it harder to add a new jackson configuration, since a new flag has to be added, new configuration options for the template etc. If we are using only one flag all those features could be implemented in one optional template block which does make it kind of trivial to keep this feature consistent. The annotations could be extracted in a annotation file which is inserted in every model. that would minimize the files that needed to be changed on a configuration edit to perhaps two template files (mapper and annotation). |
I have no problem with throwing this pull request away. I had to implement this as a temporary fix. But I do not think that I am the only one who had this issue. Also it looks like a good feature which could be added to enhance the generator. |
Which way should I implement it yet? Seems to be still in discussion / unclear if or how to implement this feature |
What about using vendor extensions instead? e.g. adding x-java-json-ignore-properties to the model to enable it. |
Is this resolved? |
Work in progress. I only did the Pojo class so far. Enums and other specialities have to be checked, too.
PR checklist
./bin/
to update Petstore sample so that CIs can verify the change. (For instance, only need to run./bin/{LANG}-petstore.sh
,./bin/openapi3/{LANG}-petstore.sh
if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in.\bin\windows\
. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.master
,4.1.x
,5.0.x
. Default:master
.Description of the PR
Added the annotation
to every generated Pojo. This feature can be turned off via the flag
disableAdditionalFieldsAnnotation
.This makes the generated code more independend from the attached default json parser.
Fixed Issues:
Technical Commitee for Java:
@bbdouglas (2017/07) @sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04)