question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`@JsonIgnoreProperties(ignoreUnknown=false)` can not override `DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES=false`

See original GitHub issue

Description

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:open
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
sohrabsarancommented, Nov 16, 2021

upvote

1reaction
noschangcommented, Mar 1, 2021

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:

@JsonConfigOverride(feature = DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, active = true)
public class MyBean { 
... 
}

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 to false during the Spring startup. However, there is one specific endpoint of the API where I need strict JSON validation, thus the need of the ignoreUnkown 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 the configure() 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ignoring new fields on JSON objects using Jackson [duplicate]
Jackson provides an annotation that can be used on class level (JsonIgnoreProperties). Add the following to the top of your class (not to...
Read more >
JsonIgnoreProperties (Jackson-annotations 2.12.0 API)
Annotation that can be used to either suppress serialization of properties (during serialization), or ignore processing of JSON properties read (during ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found