Skip to content
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

Empty options serialize to null since 2.7.4 #280

Closed
ieure opened this issue Aug 30, 2016 · 7 comments
Closed

Empty options serialize to null since 2.7.4 #280

ieure opened this issue Aug 30, 2016 · 7 comments

Comments

@ieure
Copy link

ieure commented Aug 30, 2016

I tried to upgrade Jackson in my Scala project today, and was met with several failing tests. I tracked this down to a behavior change from jackson-module-scala 2.7.3 -> 2.7.4. The issue is that empty Options now serialize to null. Prior to 2.7.3, they would be omitted from the output JSON.

Here's a project with a minimal failing testcase: https://github.com/ieure/jackson-scala-testcase

Based on this being an change in Option behavior & starting with 2.7.4, I suspect #252 is related to the breakage.

@ieure
Copy link
Author

ieure commented Aug 30, 2016

To clarify, this is still broken in 2.8.2.

@nbauernfeind
Copy link
Member

Thanks for the clarification; that was going to be my first question! I have limited time, but will see what I can do tonight.

@cowtowncoder
Copy link
Member

By default, empty Options SHOULD serialize to null unless there is specific configuration to do otherwise, as far as I know. This because default inclusion policy is JsonInclude.Include.ALWAYS.
To configure things globally, you can use any other JsonInclude.Include value except for ALWAYS or NON_NULL: so NON_EMPTY, NON_EMPTY or NON_DEFAULT should all exclude absent Option value.

To set default inclusion strategy you would use:

mapper.setSerializationInclusion(Include.NON_ABSENT)

or use @JsonInclude annotation on property in question.

Now: I don't think that a chance in patch version was intentional or the right way to do this; such fixes should be done in new minor versions. But going forward it sounds like the new behavior is the way things are supposed to work from Jackson perspective.

@ieure
Copy link
Author

ieure commented Aug 31, 2016

Hmm, NON_ABSENT seems to produce the behavior I expect, but I still find it surprising to get null in the output when I've selected Include.NON_NULL.

@cowtowncoder
Copy link
Member

@ieure null here would mean that the property itself would be null, regardless of contents.
So NON_NULL definition is explicit JVM level null of value, even for referential types (Option, AtomicReference, guava/Java8 optionals).
I agree that things get bit hairy, but as of 2.7(.4) and beyond this should be the uniform definition. And also reason why additional values were added, to allow fine-grained levels.

For 2.9 I hope to finally add ability to define custom handlers because possible rules users want are unlimited: simplest ones can be enumerated, but there needs to be an extension point to allow even more elaborate checking.

FWIW, NON_ABSENT also includes what NON_NULL does, so it's a superset of things to exclude.

Hope this helps.

@jsw
Copy link

jsw commented Sep 15, 2016

I also thought this was a bug. Would be great if this new behavior were documented. :-)

@cowtowncoder
Copy link
Member

@jsw It is documented in Javadocs for @JsonInclude, but would be great if we got better wiki for in-depth explanation. Behavior is not really new for databind in general, but I think Scala module differed from it, so I guess it is new for Scala module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants