#2253 - Incorrect JSON format for LocalDateTime. Includes Z suffix when it should not.

This commit is contained in:
rbygrave
2021-06-17 13:26:34 +12:00
parent 37a3a51b6b
commit 7af4e166a4
2 changed files with 60 additions and 11 deletions
@@ -1,7 +1,10 @@
package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import io.ebean.config.JsonConfig;
import java.io.IOException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
@@ -29,12 +32,37 @@ public class ScalarTypeLocalDateTime extends ScalarTypeBaseDateTime<LocalDateTim
@Override
protected String toJsonNanos(LocalDateTime value) {
return String.valueOf(convertToMillis(value));
return value.toString();
}
@Override
protected String toJsonISO8601(LocalDateTime dateTime) {
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toString();
protected String toJsonISO8601(LocalDateTime value) {
return value.toString();
}
@Override
protected LocalDateTime fromJsonISO8601(String value) {
return LocalDateTime.parse(value);
}
@Override
public LocalDateTime jsonRead(JsonParser parser) throws IOException {
return LocalDateTime.parse(parser.getText());
}
@Override
public void jsonWrite(JsonGenerator writer, LocalDateTime value) throws IOException {
writer.writeString(value.toString());
}
@Override
public String formatValue(LocalDateTime value) {
return value.toString();
}
@Override
public LocalDateTime parse(String value) {
return LocalDateTime.parse(value);
}
@Override
@@ -1,25 +1,28 @@
package io.ebeaninternal.server.type;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import io.ebean.config.JsonConfig;
import org.junit.Test;
import java.io.StringWriter;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
public class ScalarTypeLocalDateTimeTest {
ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.MILLIS);
private final ScalarTypeLocalDateTime type = new ScalarTypeLocalDateTime(JsonConfig.DateTime.MILLIS);
private final JsonFactory factory = new JsonFactory();
// warm up
LocalDateTime warmUp = LocalDateTime.now();
private final LocalDateTime warmUp = LocalDateTime.now();
@Test
public void testNowToMillis() throws Exception {
public void testNowToMillis() {
warmUp.hashCode();
@@ -29,7 +32,7 @@ public class ScalarTypeLocalDateTimeTest {
}
@Test
public void testConvertToMillis() throws Exception {
public void testConvertToMillis() {
LocalDateTime now = LocalDateTime.now().withNano(123_000_000); // jdk11 workaround
long asMillis = type.convertToMillis(now);
@@ -39,7 +42,7 @@ public class ScalarTypeLocalDateTimeTest {
}
@Test
public void testConvertFromTimestamp() throws Exception {
public void testConvertFromTimestamp() {
Timestamp now = new Timestamp(System.currentTimeMillis());
@@ -74,6 +77,24 @@ public class ScalarTypeLocalDateTimeTest {
assertEquals(timestamp, timestamp1);
}
@Test
public void testJsonRaw() throws Exception {
final LocalDateTime of = LocalDateTime.of(2020, 5, 4, 13, 20, 40);
ScalarTypeLocalDateTime typeIso = new ScalarTypeLocalDateTime(JsonConfig.DateTime.ISO8601);
StringWriter writer = new StringWriter();
JsonGenerator generator = factory.createGenerator(writer);
typeIso.jsonWrite(generator, of);
generator.flush();
assertThat(of.toString()).isEqualTo("2020-05-04T13:20:40");
assertThat(writer.toString()).isEqualTo("\"2020-05-04T13:20:40\"");
}
@Test
public void testJson() throws Exception {