feat: support java.time.OffsetDateTime in post data (#1624)

Fixes https://github.com/microsoft/playwright-java/issues/1623
This commit is contained in:
Yury Semikhatsky 2024-07-11 08:58:46 -07:00 коммит произвёл GitHub
Родитель 7fa8081032
Коммит 7a12897be4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 20 добавлений и 4 удалений

Просмотреть файл

@ -35,10 +35,7 @@ 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.*;
import java.util.*;
import java.util.regex.Pattern;
@ -79,6 +76,7 @@ class Serialization {
static final Gson jsonDataSerializer = new GsonBuilder().disableHtmlEscaping()
.registerTypeAdapter(Date.class, new DateSerializer())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer())
.registerTypeAdapter(OffsetDateTime.class, new OffsetDateTimeSerializer())
.serializeNulls().create();
static SerializedError serializeError(Throwable e) {
@ -519,6 +517,13 @@ class Serialization {
}
}
private static class OffsetDateTimeSerializer implements JsonSerializer<OffsetDateTime> {
@Override
public JsonElement serialize(OffsetDateTime src, Type typeOfSrc, JsonSerializationContext context) {
return new JsonPrimitive(dateFormat.format(Date.from(src.toInstant())));
}
}
private static class LocalDateTimeSerializer implements JsonSerializer<LocalDateTime> {
@Override
public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) {

Просмотреть файл

@ -28,6 +28,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ExecutionException;
@ -520,6 +521,16 @@ public class TestBrowserContextFetch extends TestBase {
assertEquals("{\"name\":\"foo\",\"localDateTime\":\"2022-12-23T06:14:58.818Z\",\"date\":\"2022-12-23T06:14:58.818Z\",\"nullLocalDateTime\":null,\"nullDate\":null}",
new String(req.get().postBody));
}
@Test
void shouldSupportOffsetDateTimeInData() throws ExecutionException, InterruptedException {
APIRequestContext request = playwright.request().newContext();
OffsetDateTime offsetDateTime = OffsetDateTime.parse("2024-07-10T10:15:30-08:00");
Future<Server.Request> serverRequest = server.futureRequest("/empty.html");
request.get(server.EMPTY_PAGE, RequestOptions.create().setData(mapOf("date", offsetDateTime)));
byte[] body = serverRequest.get().postBody;
assertEquals("{\"date\":\"2024-07-10T18:15:30.000Z\"}", new String(body));
}
@Test
void shouldSupportApplicationXWwwFormUrlencoded() throws ExecutionException, InterruptedException {