-
-
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
[Java] Add a new additional property to configure Jackson's failOnUnknownProperties
#19271
[Java] Add a new additional property to configure Jackson's failOnUnknownProperties
#19271
Conversation
@@ -322,6 +325,7 @@ public AbstractJavaCodegen() { | |||
cliOptions.add(CliOption.newString(ADDITIONAL_MODEL_TYPE_ANNOTATIONS, "Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)")); | |||
cliOptions.add(CliOption.newString(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS, "Additional annotations for oneOf interfaces(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)")); | |||
cliOptions.add(CliOption.newBoolean(OPENAPI_NULLABLE, "Enable OpenAPI Jackson Nullable library", this.openApiNullable)); | |||
cliOptions.add(CliOption.newBoolean(FAIL_ON_UNKNOWN_PROPERTIES, "Fail Jackson de-serialization on unknown properties", this.failOnUnknownProperties)); |
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.
shall we add this option to java client codegen java class only as only java client needs this option?
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.
Indeed, that's a good idea, I will make the change
@@ -30,7 +30,7 @@ public class JSON implements ContextResolver<ObjectMapper> { | |||
mapper = JsonMapper.builder() | |||
.serializationInclusion(JsonInclude.Include.NON_NULL) | |||
.configure(MapperFeature.ALLOW_COERCION_OF_SCALARS, false) | |||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true) | |||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, {{failOnUnknownProperties}}) |
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.
Hi, could you please make this change for other libraries besides retrofit2 as well?
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.
Hi, yes, I can do this, the only issue is that some libraries are configured to fail on unknown properties, some aren't.
I see that apache-httpclient
, feign
, google-api-client
, restassured
, restclient
, resteasy
, vertx
and webclient
currently have DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
set to false
, whereas jersey2
, jersey3
, native
and retrofit2
have DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
set to true
Is there an elegant way to tell JavaClientCodegen to default the failOnUnknownProperties
variable to true or false, depending of the chosen library?
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.
Good point.
My preferred approach would be to use this opportunity to unify the behaviour of the different libraries. Since the majority uses false
as default, let's make false
the default value of failOnUnknownProperties
for all libraries.
This would also make this PR a "breaking change with fallback", i.e. it should become part of OpenAPI Generator 7.8.0, not of 7.7.1. But I think wing328 will take care of this aspect.
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's done!
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.
Thank you!
Some tests in samples/openapi3/client/petstore/java/jersey2-java8/
are failing now. Apparently due to the custom deserialization logic in this sample, which relies on FAIL_ON_UNKNOWN_PROPERTIES
being true
. Can you please fix this by simply adding in bin/configs/java-jersey2-8.yaml
the following line somewhere under additionalProperties:
?
failOnUnknownProperties: true
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.
Fixed! Thanks for the hint, I had trouble finding where it came from
Add the `failOnUnknownProperties: true` additional properties to generate the expected mapper
93c9b1a
to
36f76f2
Compare
@@ -30,7 +30,7 @@ public class JSON implements ContextResolver<ObjectMapper> { | |||
mapper = JsonMapper.builder() | |||
.serializationInclusion(JsonInclude.Include.NON_NULL) | |||
.configure(MapperFeature.ALLOW_COERCION_OF_SCALARS, false) | |||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true) | |||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, {{failOnUnknownProperties}}) |
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.
are there plans to have something similar for Kotlin generators too? I got exactly the same issue using Kotlin + okhttp3 client and seems like there we use the same properties and in Java's generators.
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 can probably take a look once this PR is merged. Is there a issue already?
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.
thanks a lot. I am not aware of formal issue yet but please let me know if I should open one.
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, that would be great!
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.
here it is #19408 . Thanks!
Can we entertain the possibility of defaulting |
That would be the new default with this MR |
Amazing, thank you. I thought we were just adding a new config property. :) |
thanks for the PR, which has been merged |
@martin-mfg
This MR should fix #10306
It adds a new additional and non mandatory property to change the generated Jackson's ObjectMapper's FAIL_ON_UNKNOWN_PROPERTIES's value
If this property is not set, we keep the default value, i.e.
true
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.6.0 minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)