Skip to content

Commit 96838dc

Browse files
authored
Fix for issue 3481 (#3486)
1 parent de3d0ec commit 96838dc

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,10 @@ public void serializeAsField(Object bean, JsonGenerator gen,
689689
: _accessorMethod.invoke(bean, (Object[]) null);
690690

691691
// Null handling is bit different, check that first
692-
if (value == null) {
692+
if (value == null ) {
693+
if(_suppressableValue != null && _suppressableValue.equals(value)) {
694+
return;
695+
}
693696
if (_nullSerializer != null) {
694697
gen.writeFieldName(_name);
695698
_nullSerializer.serialize(null, gen, prov);

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

-5
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,6 @@ protected BeanPropertyWriter buildWriter(SerializerProvider prov,
204204
break;
205205
case CUSTOM: // new with 2.9
206206
valueToSuppress = prov.includeFilterInstance(propDef, inclV.getValueFilter());
207-
if (valueToSuppress == null) { // is this legal?
208-
suppressNulls = true;
209-
} else {
210-
suppressNulls = prov.includeFilterSuppressNulls(valueToSuppress);
211-
}
212207
break;
213208
case NON_NULL:
214209
suppressNulls = true;

src/test/java/com/fasterxml/jackson/databind/ser/filter/JsonIncludeCustomTest.java

+7-12
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import com.fasterxml.jackson.annotation.*;
88

99
import com.fasterxml.jackson.databind.BaseMapTest;
10+
import com.fasterxml.jackson.databind.JsonMappingException;
1011
import com.fasterxml.jackson.databind.ObjectMapper;
11-
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
1212

1313
// Tests for [databind#888]
1414
public class JsonIncludeCustomTest extends BaseMapTest
@@ -110,16 +110,12 @@ public void testRepeatedCalls() throws Exception
110110

111111
assertEquals(a2q("{'value':'x'}"),
112112
MAPPER.writeValueAsString(new CountingFooBean("x")));
113+
assertEquals(1, CountingFooFilter.counter.get());
113114

114-
// 06-May-2022, tatu: Maybe surprisingly, we get TWO calls; first one to
115-
// see if `null`s are to be filtered, second time for "real" call
116-
assertEquals(2, CountingFooFilter.counter.get());
117115
assertEquals("{}", MAPPER.writeValueAsString(new CountingFooBean("foo")));
116+
assertEquals(2, CountingFooFilter.counter.get());
118117

119-
// but beyond initial extra call, as expected
120-
assertEquals(3, CountingFooFilter.counter.get());
121-
122-
// except filter will NOT be called again for `null`s, as per [databind#3481]
118+
// except filter will be called again for `null`s, as per [databind#3481]
123119
assertEquals(a2q("{'value':null}"), MAPPER.writeValueAsString(new CountingFooBean(null)));
124120
assertEquals(3, CountingFooFilter.counter.get());
125121
}
@@ -133,11 +129,10 @@ public void testRepeatedCalls() throws Exception
133129
public void testBrokenFilter() throws Exception
134130
{
135131
try {
136-
String json = MAPPER.writeValueAsString(new BrokenBean("foo"));
132+
String json = MAPPER.writeValueAsString(new BrokenBean(null));
137133
fail("Should not pass, produced: "+json);
138-
} catch (InvalidDefinitionException e) {
139-
verifyException(e, "Problem determining whether filter of type");
140-
verifyException(e, "filter out `null`");
134+
} catch (JsonMappingException e) {
135+
verifyException(e, "while trying to invoke the method java.lang.Object.toString() of a null object loaded from local variable 'other'");
141136
}
142137
}
143138
}

0 commit comments

Comments
 (0)