Skip to content

Commit

Permalink
Fail deserialization into LocalDateTime of datetime with timezone (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelyan authored Oct 22, 2020
1 parent 14b73b6 commit 613ee4f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

import java.io.IOException;
import java.time.DateTimeException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

import com.fasterxml.jackson.annotation.JsonFormat;
Expand Down Expand Up @@ -150,15 +148,6 @@ protected LocalDateTime _fromString(JsonParser p, DeserializationContext ctxt,
}
final DateTimeFormatter format = _formatter;
try {
if (format == DEFAULT_FORMATTER) {
// JavaScript by default includes time and zone in JSON serialized Dates (UTC/ISO instant format).
if (string.length() > 10 && string.charAt(10) == 'T') {
if (string.endsWith("Z")) {
return LocalDateTime.ofInstant(Instant.parse(string), ZoneOffset.UTC);
}
return LocalDateTime.parse(string, DEFAULT_FORMATTER);
}
}
return LocalDateTime.parse(string, format);
} catch (DateTimeException e) {
return _handleDateTimeFormatException(ctxt, e, format, string);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.time.temporal.Temporal;
import java.util.Calendar;
Expand Down Expand Up @@ -169,11 +168,16 @@ public void testDeserializationAsString03() throws Exception
}

@Test
public void testDeserializationAsString04() throws Exception
public void testBadDeserializationOfTimeWithTimeZone() throws Exception
{
Instant instant = Instant.now();
LocalDateTime value = MAPPER.readValue('"' + instant.toString() + '"', LocalDateTime.class);
assertEquals("The value is not correct.", LocalDateTime.ofInstant(instant, ZoneOffset.UTC), value);
try {
Instant instant = Instant.now();
MAPPER.readValue('"' + instant.toString() + '"', LocalDateTime.class);
fail("expected fail");
} catch (InvalidFormatException e) {
verifyException(e, "Cannot deserialize value of type");
verifyException(e, "from String \"");
}
}

@Test
Expand Down

0 comments on commit 613ee4f

Please sign in to comment.