Skip to content

Commit 88da63b

Browse files
committed
Fix #4262: handle null insert fail for TreeSet
1 parent 51a11bd commit 88da63b

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Project: jackson-databind
2525
#4248: `ThrowableDeserializer` does not handle `null` well for `cause`
2626
#4250: Add input validation for `NumberDeserializers` deserializers
2727
for "stringified" FP numbers
28+
#4262: Improve handling of `null` insertion failure for `TreeSet`
2829
#4263: Change `ObjectArrayDeserializer` to use "generic" type parameter
2930
(`java.lang.Object`) to remove co-variant return type
3031

src/main/java/com/fasterxml/jackson/databind/deser/std/CollectionDeserializer.java

+29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import java.util.Objects;
66

77
import com.fasterxml.jackson.annotation.JsonFormat;
8+
89
import com.fasterxml.jackson.core.*;
10+
911
import com.fasterxml.jackson.databind.*;
1012
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
1113
import com.fasterxml.jackson.databind.cfg.CoercionAction;
@@ -355,6 +357,10 @@ protected Collection<Object> _deserializeFromArray(JsonParser p, Deserialization
355357
continue;
356358
}
357359
value = _nullProvider.getNullValue(ctxt);
360+
if (value == null) {
361+
_tryToAddNull(p, ctxt, result);
362+
continue;
363+
}
358364
} else if (typeDeser == null) {
359365
value = valueDes.deserialize(p, ctxt);
360366
} else {
@@ -407,6 +413,10 @@ protected final Collection<Object> handleNonArray(JsonParser p, DeserializationC
407413
return result;
408414
}
409415
value = _nullProvider.getNullValue(ctxt);
416+
if (value == null) {
417+
_tryToAddNull(p, ctxt, result);
418+
return result;
419+
}
410420
} else if (typeDeser == null) {
411421
value = valueDes.deserialize(p, ctxt);
412422
} else {
@@ -469,6 +479,25 @@ protected Collection<Object> _deserializeWithObjectId(JsonParser p, Deserializat
469479
return result;
470480
}
471481

482+
/**
483+
* {@code java.util.TreeSet} does not allow addition of {@code null} values,
484+
* so isolate handling here.
485+
*
486+
* @since 2.17
487+
*/
488+
protected void _tryToAddNull(JsonParser p, DeserializationContext ctxt, Collection<?> set)
489+
throws IOException
490+
{
491+
// Ideally we'd have better idea of where nulls are accepted, but first
492+
// let's just produce something better than NPE:
493+
try {
494+
set.add(null);
495+
} catch (NullPointerException e) {
496+
ctxt.handleUnexpectedToken(_valueType, JsonToken.VALUE_NULL, p,
497+
"`java.util.Collection` of type %s does not accept `null` values",
498+
ClassUtil.getTypeDescription(getValueType(ctxt)));
499+
}
500+
}
472501
/**
473502
* Helper class for dealing with Object Id references for values contained in
474503
* collections being deserialized.

src/test/java/com/fasterxml/jackson/databind/deser/jdk/TestEmptyArrayBlockingQueueDeser.java renamed to src/test/java/com/fasterxml/jackson/databind/deser/jdk/EmptyArrayBlockingQueueDeserTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import com.fasterxml.jackson.databind.*;
77

8-
public class TestEmptyArrayBlockingQueueDeser extends BaseMapTest
8+
public class EmptyArrayBlockingQueueDeserTest extends BaseMapTest
99
{
1010
static class RemoteEntity{
1111
private Collection<Double> values = new ArrayBlockingQueue<>(20);

src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKCollectionsDeserTest.java

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.fasterxml.jackson.core.type.TypeReference;
88

99
import com.fasterxml.jackson.databind.*;
10+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
1011
import com.fasterxml.jackson.databind.testutil.NoCheckSubTypeValidator;
1112

1213
/**
@@ -64,4 +65,16 @@ public void testUnmodifiableSet() throws Exception
6465
assertNotNull(result);
6566
assertEquals(1, result.size());
6667
}
68+
69+
// [databind#4262]: Handle problem of `null`s for `TreeSet`
70+
public void testNullsWithTreeSet() throws Exception
71+
{
72+
try {
73+
MAPPER.readValue("[ \"acb\", null, 123 ]", TreeSet.class);
74+
fail("Should not pass");
75+
} catch (MismatchedInputException e) {
76+
verifyException(e, "`java.util.Collection` of type ");
77+
verifyException(e, " does not accept `null` values");
78+
}
79+
}
6780
}

0 commit comments

Comments
 (0)