Skip to content

Commit

Permalink
backwards compatiblity - throwing DateTimeException where needed, fix…
Browse files Browse the repository at this point in the history
…ing exception messages

- Changed JsonParseException to DateTimeException where needed
- Extended bounds of min and max Duration to be Long.MAX and Long.MIN
- Adjusting DateTimeException message to match original for Instant
- Adjusting DurationDeserializer DateTimeException message to something similar as Instance
  • Loading branch information
abracadv8 committed Oct 9, 2018
1 parent 6ac19f6 commit e58de98
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.fasterxml.jackson.datatype.jsr310.deser;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.JsonTokenId;
Expand All @@ -41,10 +40,10 @@ public class DurationDeserializer extends JSR310DeserializerBase<Duration>

public static final DurationDeserializer INSTANCE = new DurationDeserializer();

private static final BigDecimal INSTANT_MAX = new BigDecimal(
java.time.Instant.MAX.getEpochSecond() + "." + java.time.Instant.MAX.getNano());
private static final BigDecimal INSTANT_MIN = new BigDecimal(
java.time.Instant.MIN.getEpochSecond() + "." + java.time.Instant.MIN.getNano());
private static final BigDecimal DURATION_MAX = new BigDecimal(
Long.MAX_VALUE + "." + java.time.Instant.MAX.getNano());
private static final BigDecimal DURATION_MIN = new BigDecimal(
Long.MIN_VALUE + "." + java.time.Instant.MIN.getNano());

private DurationDeserializer()
{
Expand All @@ -58,11 +57,11 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t
{
case JsonTokenId.ID_NUMBER_FLOAT:
BigDecimal value = parser.getDecimalValue();
// If the decimal isnt within the bounds of a float, bail out
if(value.compareTo(INSTANT_MAX) > 0 ||
value.compareTo(INSTANT_MIN) < 0) {
throw new JsonParseException(context.getParser(),
"Value of Float too large to be converted to Duration");
// If the decimal isn't within the bounds of a duration, bail out
if(value.compareTo(DURATION_MAX) > 0 ||
value.compareTo(DURATION_MIN) < 0) {
throw new DateTimeException(
"Instant exceeds minimum or maximum Duration");
}

long seconds = value.longValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,13 @@ protected T _fromLong(DeserializationContext context, long timestamp)
timestamp, this.getZone(context)));
}

protected T _fromDecimal(DeserializationContext context, BigDecimal value) throws JsonParseException
protected T _fromDecimal(DeserializationContext context, BigDecimal value)
{
// If the decimal isnt within the bounds of an Instant, bail out
if(value.compareTo(INSTANT_MAX) > 0 ||
value.compareTo(INSTANT_MIN) < 0) {
throw new JsonParseException(context.getParser(),
"Value of String too large to be converted to Instant");
throw new DateTimeException(
"Instant exceeds minimum or maximum instant");
}

long seconds = value.longValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.fasterxml.jackson.datatype.jsr310;

import java.math.BigInteger;
import java.time.DateTimeException;
import java.time.Duration;
import java.time.temporal.TemporalAmount;

import com.fasterxml.jackson.core.JsonParseException;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -53,28 +54,36 @@ public void testDeserializationAsFloat03() throws Exception
assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 8374), value);
}

@Test
public void testDeserializationAsFloat04() throws Exception
{
Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)
.readValue("13498.000008374");
assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 8374), value);
}

@Test(expected = DateTimeException.class)
public void testDeserializationAsFloat05() throws Exception
{
String customInstant = new BigInteger(Long.toString(Long.MAX_VALUE)).add(BigInteger.ONE) + ".0";
READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)
.readValue(customInstant);
}

/**
* This test can potentially hang the VM, so exit if it doesn't finish
* within a few seconds.
* @throws Exception
*/
@Test(timeout=3000, expected = JsonParseException.class)
@Test(timeout=3000, expected = DateTimeException.class)
public void testDeserializationAsFloatWhereStringTooLarge() throws Exception
{
String customDuration = "1000000000e1000000000";
READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)
.readValue(customDuration);
}

@Test
public void testDeserializationAsFloat04() throws Exception
{
Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)
.readValue("13498.000008374");
assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 8374), value);
}

@Test
public void testDeserializationAsInt01() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.Test;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
Expand Down Expand Up @@ -418,7 +414,7 @@ public void testDeserializationWithTypeInfoAndStringTooLarge01() throws Exceptio
*
* @throws Exception
*/
@Test(timeout=3000, expected = JsonParseException.class)
@Test(timeout=3000, expected = DateTimeException.class)
public void testDeserializationWithTypeInfoAndStringTooLarge02() throws Exception
{
Instant date = Instant.MAX;
Expand Down

0 comments on commit e58de98

Please sign in to comment.