Skip to content

Commit d6f0a9e

Browse files
authored
When JsonInclude.NON_DEFAULT is configured, at least NON_EMPTY should be configured. (#4467)
1 parent e6145c3 commit d6f0a9e

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,3 +1771,7 @@ Oddbjørn Kvalsund (oddbjornkvalsund@github)
17711771
in `DeserializerCache` to avoid deadlock on pinning
17721772
(2.17.1)
17731773

1774+
Teodor Danciu (teodord@github)
1775+
* Repored #4464: When `Include.NON_DEFAULT` setting is used, `isEmpty()` method is
1776+
not called on the serializer
1777+
(2.18.0)

release-notes/VERSION-2.x

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

77
2.18.0 (not yet released)
88

9-
-
9+
#4464: When `Include.NON_DEFAULT` setting is used, `isEmpty()` method is
10+
not called on the serializer
11+
(reported by Teodor D)
12+
(fix by Joo-Hyuk K)
1013

1114
2.17.1 (not yet released)
1215

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ 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;
185187
} else {
186188
if (valueToSuppress.getClass().isArray()) {
187189
valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.fasterxml.jackson.databind.ser.filter;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.core.JsonGenerator;
5+
import com.fasterxml.jackson.databind.JsonSerializer;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.databind.SerializerProvider;
8+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9+
import com.fasterxml.jackson.databind.json.JsonMapper;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.io.IOException;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
16+
// [databind#4464] Since 2.15, NON_DEFAULT should be extension of NON_EMPTY
17+
public class JsonInclude4464Test {
18+
19+
public static class BarSerializer extends JsonSerializer<Bar> {
20+
21+
public BarSerializer() {
22+
}
23+
24+
@Override
25+
public void serialize(Bar value, JsonGenerator gen, SerializerProvider provider) throws IOException {
26+
gen.writeObject(value);
27+
}
28+
29+
@Override
30+
public boolean isEmpty(SerializerProvider provider, Bar value) {
31+
return "I_AM_EMPTY".equals(value.getName());
32+
}
33+
}
34+
35+
public static class Bar {
36+
public String getName() {
37+
return "I_AM_EMPTY";
38+
}
39+
}
40+
41+
public static class Foo {
42+
@JsonSerialize(using = BarSerializer.class)
43+
public Bar getBar() {
44+
return new Bar();
45+
}
46+
}
47+
48+
@Test
49+
public void test86() throws IOException {
50+
ObjectMapper mapper = JsonMapper.builder().serializationInclusion(JsonInclude.Include.NON_DEFAULT).build();
51+
String json = mapper.writeValueAsString(new Foo());
52+
assertEquals("{}", json);
53+
}
54+
}

0 commit comments

Comments
 (0)