Skip to content

Commit 3da3da8

Browse files
authored
Regression fix for #4741 Include.NON_DEFAULT setting used on POJO, empty values are not included in json if default is null (#4744)
1 parent 80e84c0 commit 3da3da8

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

release-notes/VERSION-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Project: jackson-databind
66

77
2.18.1 (WIP-2024)
88

9+
#4741: When `Include.NON_DEFAULT` setting is used on POJO, empty values
10+
are not included in json if default is `null`
11+
(reported by @ragnhov)
12+
(fix by Joo-Hyuk K)
913
#4749: Fixed a problem with `StdDelegatingSerializer#serializeWithType` looking up the serializer
1014
with the wrong argument
1115
(fix by wrongwrong)

src/main/java/com/fasterxml/jackson/databind/ser/PropertyBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,12 @@ protected BeanPropertyWriter buildWriter(SerializerProvider prov,
182182
}
183183
if (valueToSuppress == null) {
184184
suppressNulls = true;
185-
// [databind#4464] NON_DEFAULT does not work with NON_EMPTY for custom serializer
186-
valueToSuppress = BeanPropertyWriter.MARKER_FOR_EMPTY;
185+
// [databind#4471] Different behavior when Include.NON_DEFAULT
186+
// setting is used on POJO vs global setting, as per documentation.
187+
if (!_useRealPropertyDefaults) {
188+
// [databind#4464] NON_DEFAULT does not work with NON_EMPTY for custom serializer
189+
valueToSuppress = BeanPropertyWriter.MARKER_FOR_EMPTY;
190+
}
187191
} else {
188192
if (valueToSuppress.getClass().isArray()) {
189193
valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fasterxml.jackson.databind.ser.filter;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import com.fasterxml.jackson.annotation.JsonInclude;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
public class JsonInclude4741Test
12+
extends DatabindTestUtil
13+
{
14+
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
15+
public static class MyString {
16+
public String value = null;
17+
}
18+
19+
private final ObjectMapper MAPPER = newJsonMapper();
20+
21+
@Test
22+
void testSerialization() throws Exception
23+
{
24+
MyString input = new MyString();
25+
input.value = "";
26+
27+
String json = MAPPER.writeValueAsString(input);
28+
assertEquals(a2q("{'value':''}"), json);
29+
30+
MyString output = MAPPER.readValue(json, MyString.class);
31+
32+
assertEquals(input.value, output.value);
33+
}
34+
}

0 commit comments

Comments
 (0)