Skip to content

Commit

Permalink
fix: Date and LocalDateTime serialization in post data (microsoft#1150)
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s authored Dec 23, 2022
1 parent ec5f960 commit 4246c37
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Pattern;

Expand All @@ -43,6 +47,8 @@

class Serialization {
private static final Gson gson = new GsonBuilder().disableHtmlEscaping()
.registerTypeAdapter(Date.class, new DateSerializer())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer())
.registerTypeAdapter(SameSiteAttribute.class, new SameSiteAdapter().nullSafe())
.registerTypeAdapter(BrowserChannel.class, new ToLowerCaseAndDashSerializer<BrowserChannel>())
.registerTypeAdapter(ColorScheme.class, new ToLowerCaseAndDashSerializer<ColorScheme>())
Expand Down Expand Up @@ -468,5 +474,29 @@ public SameSiteAttribute read(JsonReader in) throws IOException {
return SameSiteAttribute.valueOf(value.toUpperCase());
}
}

private static DateFormat iso8601Format() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat;
}
private static final DateFormat dateFormat = iso8601Format();

private static class DateSerializer implements JsonSerializer<Date> {
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(dateFormat.format(src));
}
}

private static class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime> {
@Override
public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) {
ZoneOffset offset = ZoneId.systemDefault().getRules().getOffset(src);
Instant instant = src.toInstant(offset);
return new JsonPrimitive(dateFormat.format(Date.from(instant)));
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -479,6 +482,28 @@ void shouldOverrideRequestParameters() throws ExecutionException, InterruptedExc
assertEquals("data", new String(req.get().postBody));
}

public static class TestData {
public String name;
public LocalDateTime localDateTime;
public Date date;
public LocalDateTime nullLocalDateTime;
public Date nullDate;
}

@Test
void shouldSerializeDateAndLocalDateTime() throws ExecutionException, InterruptedException, ParseException {
Request pageReq = page.waitForRequest("**/*", () -> page.navigate(server.EMPTY_PAGE));
Future<Server.Request> req = server.futureRequest("/empty.html");
TestData testData = new TestData();
testData.name = "foo";
long currentMillis = 1671776098818L;
testData.date = new Date(currentMillis);
testData.localDateTime = testData.date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
context.request().fetch(pageReq, RequestOptions.create().setMethod("POST").setData(testData));
assertEquals("{\"name\":\"foo\",\"localDateTime\":\"2022-12-23T06:14:58.818Z\",\"date\":\"2022-12-23T06:14:58.818Z\"}",
new String(req.get().postBody));
}

@Test
void shouldSupportApplicationXWwwFormUrlencoded() throws ExecutionException, InterruptedException {
Future<Server.Request> req = server.futureRequest("/empty.html");
Expand Down

0 comments on commit 4246c37

Please sign in to comment.