Skip to content

Fail deserialization into LocalDateTime of datetime with timezone #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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