fix(fetch): serialize LocalDate in post data (#1934)
This commit is contained in:
@@ -77,6 +77,7 @@ class Serialization {
|
||||
|
||||
static final Gson jsonDataSerializer = new GsonBuilder().disableHtmlEscaping()
|
||||
.registerTypeAdapter(Date.class, new DateSerializer())
|
||||
.registerTypeAdapter(LocalDate.class, new LocalDateSerializer())
|
||||
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer())
|
||||
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeSerializer())
|
||||
.serializeNulls().create();
|
||||
@@ -571,6 +572,15 @@ class Serialization {
|
||||
}
|
||||
}
|
||||
|
||||
private static class LocalDateSerializer implements JsonSerializer<LocalDate> {
|
||||
@Override
|
||||
public JsonElement serialize(LocalDate src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
// LocalDate has no time or zone, so emit the ISO-8601 date (yyyy-MM-dd) as-is to
|
||||
// avoid shifting the calendar date when converting through a time zone.
|
||||
return new JsonPrimitive(src.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private static class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime> {
|
||||
@Override
|
||||
public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.text.ParseException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
@@ -533,6 +534,23 @@ public class TestBrowserContextFetch extends TestBase {
|
||||
assertEquals("{\"date\":\"2024-07-10T18:15:30.000Z\"}", new String(body));
|
||||
}
|
||||
|
||||
public static class LocalDateData {
|
||||
public String name;
|
||||
public LocalDate date;
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSupportLocalDateInData() throws ExecutionException, InterruptedException {
|
||||
APIRequestContext request = playwright.request().newContext();
|
||||
LocalDateData testData = new LocalDateData();
|
||||
testData.name = "foo";
|
||||
testData.date = LocalDate.of(2022, 12, 23);
|
||||
Future<Server.Request> serverRequest = server.futureRequest("/empty.html");
|
||||
request.post(server.EMPTY_PAGE, RequestOptions.create().setData(testData));
|
||||
byte[] body = serverRequest.get().postBody;
|
||||
assertEquals("{\"name\":\"foo\",\"date\":\"2022-12-23\"}", new String(body));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSupportApplicationXWwwFormUrlencoded() throws ExecutionException, InterruptedException {
|
||||
Future<Server.Request> req = server.futureRequest("/empty.html");
|
||||
|
||||
Reference in New Issue
Block a user