Skip to content

Commit

Permalink
Fix #129
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Aug 18, 2019
1 parent 8654d6c commit 230750f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.util.ClassUtil;

@SuppressWarnings("serial")
public abstract class JSR310DateTimeDeserializerBase<T>
Expand Down Expand Up @@ -125,6 +126,7 @@ protected T _failForNotLenient(JsonParser p, DeserializationContext ctxt,
JsonToken expToken) throws IOException
{
return (T) ctxt.handleUnexpectedToken(handledType(), expToken, p,
"not allowed because 'strict' mode set for property or type (enabled 'lenient' handling to allow)");
"Cannot deserialize instance of %s out of %s token: not allowed because 'strict' mode set for property or type (enable 'lenient' handling to allow)",
ClassUtil.nameOf(handledType()), p.currentToken());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,49 +80,85 @@ public void testDeserializationAsString01() throws Exception
}

@Test
public void testDeserializationAsString03() throws Exception
public void testDeserializationAsString02() throws Exception
{
LocalDateTime date = LocalDateTime.now();
LocalDate value = MAPPER.readValue('"' + date.toString() + '"', LocalDate.class);

assertNotNull("The value should not be null.", value);
assertEquals("The value is not correct.", date.toLocalDate(), value);
}

@Test(expected = JsonMappingException.class)
public void testDeserializationAsString04() throws Exception
{
this.MAPPER.readValue("\"2015-06-19TShouldNotParse\"", LocalDate.class);
assertEquals("The value is not correct.", date.toLocalDate(),
READER.readValue('"' + date.toString() + '"'));
}

@Test
public void testDeserializationAsString05() throws Exception
public void testDeserializationAsString03() throws Exception
{
Instant instant = Instant.now();
LocalDate value = READER.readValue('"' + instant.toString() + '"');
assertEquals("The value is not correct.",
LocalDateTime.ofInstant(instant, ZoneOffset.UTC).toLocalDate(),
value);
}

@Test
public void testBadDeserializationAsString01() throws Throwable
{
try {
READER.readValue(quote("notalocaldate"));
fail("expected DateTimeParseException");
fail("Should not pass");
} catch (MismatchedInputException e) {
verifyException(e, "Cannot deserialize value of type");
verifyException(e, "from String \"");
}
}

@Test
public void testBadDeserializationAsString02() throws Exception
{
try {
READER.readValue(quote("2015-06-19TShouldNotParse"));
fail("Should not pass");
} catch (JsonMappingException e) {
verifyException(e, "Cannot deserialize value of type");
verifyException(e, "from String \"");
}
}

/*
/**********************************************************
/* Deserialization from alternate representations
/* Deserialization from alternate representation: int (number
/* of days since Epoch)
/**********************************************************
*/

// By default, lenient handling on so we can do this:
@Test
public void testLenientDeserializeFromInt() throws Exception
{
assertEquals("The value is not correct.", LocalDate.of(1970, Month.JANUARY, 3),
READER.readValue("2"));

assertEquals("The value is not correct.", LocalDate.of(1970, Month.FEBRUARY, 10),
READER.readValue("40"));
}

// But with alternate setting, not so
@Test
public void testStricDeserializeFromInt() throws Exception
{
ObjectMapper mapper = mapperBuilder()
.build();
mapper.configOverride(LocalDate.class)
.setFormat(JsonFormat.Value.forLeniency(false));
try {
mapper.readValue("2", LocalDate.class);
fail("Should not pass");
} catch (JsonMappingException e) {
verifyException(e, "Cannot deserialize instance of");
verifyException(e, "not allowed because 'strict' mode set for property or type");
}

// 17-Aug-2019, tatu: Should possibly test other mechanism too, but for now let's
// be content with just one...
}

/*
/**********************************************************
/* Tests for alternate array handling
Expand Down
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Modules:
2.10.0.pr2

#126: Change auto-registration in 2.10 to provide "new" (JavaTimeModule) instead of legacy module
#129: Support `lenient` setting with `LocalDateDeserializer`
(suggested by esHack@github)

2.10.0.pr1 (19-Jul-2019)

Expand Down

0 comments on commit 230750f

Please sign in to comment.