Skip to content

JsonValue and JsonInclude.Include.NON_EMPTY conflicting in Jackson 2.12.5? #3285

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

Closed
andrewlandfried opened this issue Sep 24, 2021 · 3 comments

Comments

@andrewlandfried
Copy link

Describe the bug
In upgrading to 2.12.5 from Spring Boot 2.4.9's Jackson version (2.11.4), I realized an object is getting serialized differently than it was before.
I am using mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY) on my mapper.
The object I want serialized has a @JsonValue annotation on a String returning method. In 2.11.4, when the return value of that method returns a blank string "", it is serialized. In 2.12.5, the "" is not serialized.

Version information
Which Jackson version(s) was this for?
Jackson 2.11.4 --> Jackson 2.12.5.

To Reproduce
My testing code:

// parent class
@Data
@AllArgsConstructor
@NoArgsConstructor
static class ParentObj {
    private SimpleTestObject simpleObj;
    private int num;
}

// problem class
@Data
@AllArgsConstructor
@NoArgsConstructor
static class SimpleTestObject {
    private String str;

    @JsonValue
    public String toString() {
        return str;
    }
}

// Call the following for the 2 Jackson versions ...
// ObjectMapper mapper = new ObjectMapper();
// mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

// SimpleTestObject obj = new SimpleTestObject() ...  [and modify its string for different calls]
// ParentObj parent = new ParentObj(obj, 1);
// print mapper.writeValueAsString(parent)

// Results in 2.11.4: 
// with str = null --> "{"simpleObj":null,"num":1}"
// with str = "" (empty string) --> "{"simpleObj":"","num":1}"
// with str = "hi" --> "{"simpleObj":"hi","num":1}"

// Results in 2.12.5
// with str = null --> "{"num":1}"
// with str = "" (empty string) --> "{"num":1}"
// with str = "hi" --> "{"simpleObj":"hi","num":1}"

Expected behavior
Is this expected? For what it's worth, the serialization is the SAME across versions when @JsonValue is not used.

Additional context
I looked for some sort of change in @JsonValue annotation and in Serialization in 2.12 but I didn't see anything obvious that would cause this. The main reason I upgraded was to try the Blackbird module but found this as a side effect and wanted to find out if it was intended or expected.

@andrewlandfried andrewlandfried added the to-evaluate Issue that has been received but not yet evaluated label Sep 24, 2021
@cowtowncoder
Copy link
Member

I think this was due to fix to #2909. So inclusion should have been considered but was being ignored.

@cowtowncoder cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label Sep 28, 2021
@andrewlandfried
Copy link
Author

Okay so just to confirm the intention, it is expected that @JsonValue respects serialization inclusion rules in 2.12+? And the serialization inclusion rules are applied after the @JsonValue is returned?

If so, I suppose this can be closed.

@cowtowncoder
Copy link
Member

Correct. Supporting various things for @JsonValue annotated values is tricky but the intent here is to make it appear similar to how regular properties' inclusion would work (as if value was included as property and not as replacement for containing type).

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

2 participants