`@JsonIgnoreProperties(ignoreUnknown=false)` can not override `DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES=false`
See original GitHub issueDescription
I’m not sure if this is a bug or a desired behavior, but in my opinion it’s a bug. Here’s what happens:
When you set DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
to false
in an object mapper, the observed behavior is the expected, no exceptions are thrown, even if the JSON contains unknown properties.
However, if in combination with this config you annotate a bean with @JsonIgnoreProperties(ignoreUnknown = false)
, the behavior should change (in my opinion), since the annotation takes precedence over the configuration in the object mapper.
The annotation clearly states that the unknown properties should not be ignored, so, the expected behavior is that an excpetion is thrown.
I’ve searched the repository issues, but couldn’t find this problem reported anywhere. Maybe, the only issue related to this is #1523, but it needs further investigation to confirm.
Version information
Tested with jackson-databind 2.12.1
To Reproduce
It’s kinda hard to explain here how to reproduce. So, I created a project with some test that you can run to verify the behavior: JacksonBugTest.zip
Import the project on your IDE (Gradle support/plugins must be enabled) or run the tests with:
Linux: ./gradlew test -i
Windows: gradlew.bat test -i
Alternatively, you can view the report generated by my test run: test-report.zip
Feel free to contact me if you have any doubts: noschang@univali.br Thank you for your attention and congratulations for the excellent work you have been doing for the community!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:10 (5 by maintainers)
Top GitHub Comments
upvote
Hi @cowtowncoder.
I understand that changing this on 2.x would break many things. So, for a temporary solution, what about a new annotation? I thought about this:
Using this annotation would have the same effect that calling
objectMapper.config(...)
prior to the object serialization/deserialization. Maybe it’s not the best solution, but can help to solve the problem. Also it would allow to change other settings in a per object basis.Also, I don’t know if anyone else has this problem. I’m developing an API with Spring Boot and I configured the object mapper to ignore the unknown properties for all the endpoints setting
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
tofalse
during the Spring startup. However, there is one specific endpoint of the API where I need strict JSON validation, thus the need of theignoreUnkown
flag.If I had control over the object mapper in this specific endpoint, I could call the
configure()
method on it before serializing. But the object being serialized is a@RequestBody
and Spring does this under the hood. Even If I could inject the object mapper and call theconfigure()
method on it, I assume Spring uses the same instance for all the application. So, if I called the method in this endpoint I would be changing the object mapper behavior for all the application, which is not intended.