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

Regression fix for #4741 Include.NON_DEFAULT setting used on POJO, empty values are not included in json if default is null #4744

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Project: jackson-databind

2.18.1 (WIP-2024)

#4741: When `Include.NON_DEFAULT` setting is used on POJO, empty values
are not included in json if default is `null`
(reported by @ragnhov)
(fix by Joo-Hyuk K)
#4749: Fixed a problem with `StdDelegatingSerializer#serializeWithType` looking up the serializer
with the wrong argument
(fix by wrongwrong)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ protected BeanPropertyWriter buildWriter(SerializerProvider prov,
}
if (valueToSuppress == null) {
suppressNulls = true;
// [databind#4464] NON_DEFAULT does not work with NON_EMPTY for custom serializer
valueToSuppress = BeanPropertyWriter.MARKER_FOR_EMPTY;
// [databind#4471] Different behavior when Include.NON_DEFAULT
// setting is used on POJO vs global setting, as per documentation.
if (!_useRealPropertyDefaults) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of re-checking configuration settings, let's rely on "_useRealPropertyDefaults" which means that POJO settings are in use (not global-, per-type or property)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👌🏼

// [databind#4464] NON_DEFAULT does not work with NON_EMPTY for custom serializer
valueToSuppress = BeanPropertyWriter.MARKER_FOR_EMPTY;
}
} else {
if (valueToSuppress.getClass().isArray()) {
valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.fasterxml.jackson.databind.ser.filter;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JsonInclude4741Test
extends DatabindTestUtil
{
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public static class MyString {
public String value = null;
}

private final ObjectMapper MAPPER = newJsonMapper();

@Test
void testSerialization() throws Exception
{
MyString input = new MyString();
input.value = "";

String json = MAPPER.writeValueAsString(input);
assertEquals(a2q("{'value':''}"), json);

MyString output = MAPPER.readValue(json, MyString.class);

assertEquals(input.value, output.value);
}
}