Skip to content

Commit

Permalink
feat: support java.time.OffsetDateTime in post data (#1624)
Browse files Browse the repository at this point in the history
Fixes #1623
  • Loading branch information
yury-s authored Jul 11, 2024
1 parent 7fa8081 commit 7a12897
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@
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;

Expand Down Expand Up @@ -79,6 +76,7 @@ static Gson gson() {
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) {
Expand Down Expand Up @@ -519,6 +517,13 @@ public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext
}
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -520,6 +521,16 @@ void shouldSerializeDateAndLocalDateTime() throws ExecutionException, Interrupte
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 {
Expand Down

0 comments on commit 7a12897

Please sign in to comment.