Skip to content

Commit

Permalink
Fixes #142: handle nulls for RangeSet (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder authored Dec 21, 2023
1 parent 18834a6 commit 1f81df0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package com.fasterxml.jackson.datatype.guava.deser;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.type.TypeFactory;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.type.TypeFactory;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.type.LogicalType;

import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
Expand Down Expand Up @@ -95,10 +98,35 @@ public RangeSet<Comparable<?>> deserialize(JsonParser p, DeserializationContext
final Collection<?> ranges = (Collection<?>) _deserializer.deserialize(p, ctxt);
ImmutableRangeSet.Builder<Comparable<?>> builder = ImmutableRangeSet.builder();
for (Object ob : ranges) {
if (ob == null) {
_tryToAddNull(p, ctxt, builder);
continue;
}
@SuppressWarnings("unchecked")
Range<Comparable<?>> range = (Range<Comparable<?>>) ob;
builder.add(range);
}
return builder.build();
}

/**
* Some/many Guava containers do not allow addition of {@code null} values,
* so isolate handling here.
*
* @since 2.17
*/
protected void _tryToAddNull(JsonParser p, DeserializationContext ctxt,
ImmutableRangeSet.Builder<Comparable<?>> builder)
throws IOException
{
// Ideally we'd have better idea of where nulls are accepted, but first
// let's just produce something better than NPE:
try {
builder.add(null);
} catch (NullPointerException e) {
ctxt.handleUnexpectedToken(_valueType, JsonToken.VALUE_NULL, p,
"Guava `RangeSet` does not accept `null` values");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,4 @@ protected void _tryToAddNull(JsonParser p, DeserializationContext ctxt,
ClassUtil.classNameOf(cache));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.Range;
Expand Down Expand Up @@ -48,4 +49,17 @@ public void testSerializeDeserializeImmutableRangeSet() throws Exception {
assertEquals(rangeSet, MAPPER.readValue(json, new TypeReference<RangeSet<Integer>>() {}));
assertEquals(rangeSet, MAPPER.readValue(json, new TypeReference<ImmutableRangeSet<Integer>>() {}));
}

// [datatypes-collections#142]: nulls in RangeSet JSON
public void testDeserializeFromNull() throws Exception
{
final String json = a2q("[ {'lowerEndpoint':1,'lowerBoundType':'CLOSED'}, null ]");
try {
RangeSet<?> rs = MAPPER.readValue(json,
new TypeReference<ImmutableRangeSet<Integer>>() {});
fail("Should not pass, got: "+rs);
} catch (MismatchedInputException e) {
verifyException(e, "Guava `RangeSet` does not accept `null` values");
}
}
}
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Active Maintainers:
#138 (guava) `GuavaCollectionDeserializer` still throws NPE in some circumstances
(contributed by Arthur C)
#140 (guava) `Cache` deserialization fails with NPE for `null` valued entries
#142 (guava) `RangeSet` deserializer fails for content `null`s with NPE

2.16.0 (15-Nov-2023)

Expand Down

0 comments on commit 1f81df0

Please sign in to comment.