Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update OffsetDateTime Handling during Deserialization #13340

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.util.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQueries;

/**
* Custom deserializer that handles converting ISO8601 dates into {@link OffsetDateTime} objects.
*/
class DateTimeDeserializer extends JsonDeserializer<OffsetDateTime> {
private static final SimpleModule MODULE;

static {
MODULE = new SimpleModule().addDeserializer(OffsetDateTime.class, new DateTimeDeserializer());
}

/**
* Gets a module wrapping this deserializer as an adapter for the Jackson ObjectMapper.
*
* @return A {@link SimpleModule} to be plugged onto Jackson ObjectMapper.
*/
public static SimpleModule getModule() {
return MODULE;
}

@Override
public OffsetDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException {
return deserialize(jsonParser.getValueAsString());
}

static OffsetDateTime deserialize(String offsetDateTimeString) {
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
TemporalAccessor temporal = DateTimeFormatter.ISO_DATE_TIME
.parseBest(offsetDateTimeString, OffsetDateTime::from, LocalDateTime::from);
if (temporal.query(TemporalQueries.offset()) == null) {
return LocalDateTime.from(temporal).atOffset(ZoneOffset.UTC);
} else {
return OffsetDateTime.from(temporal);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@
* Custom serializer for serializing {@link OffsetDateTime} object into ISO8601 formats.
*/
final class DateTimeSerializer extends JsonSerializer<OffsetDateTime> {
private static final SimpleModule MODULE;

static {
MODULE = new SimpleModule().addSerializer(OffsetDateTime.class, new DateTimeSerializer());
}

/**
* Gets a module wrapping this serializer as an adapter for the Jackson
* ObjectMapper.
* Gets a module wrapping this serializer as an adapter for the Jackson ObjectMapper.
*
* @return a simple module to be plugged onto Jackson ObjectMapper.
*/
public static SimpleModule getModule() {
SimpleModule module = new SimpleModule();
module.addSerializer(OffsetDateTime.class, new DateTimeSerializer());
return module;
return MODULE;
}

@Override
Expand All @@ -41,6 +44,7 @@ public void serialize(OffsetDateTime value, JsonGenerator jgen, SerializerProvid

/**
* Convert the provided OffsetDateTime to its String representation.
*
* @param offsetDateTime The OffsetDateTime to convert.
* @return The String representation of the provided offsetDateTime.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ private static <T extends ObjectMapper> T initializeObjectMapper(T mapper) {
.registerModule(ByteArraySerializer.getModule())
.registerModule(Base64UrlSerializer.getModule())
.registerModule(DateTimeSerializer.getModule())
.registerModule(DateTimeDeserializer.getModule())
.registerModule(DateTimeRfc1123Serializer.getModule())
.registerModule(DurationSerializer.getModule())
.registerModule(HttpHeadersSerializer.getModule())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.util.serializer;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Tests for {@link DateTimeDeserializer}.
*/
public class DateTimeDeserializerTests {
@ParameterizedTest
@MethodSource("deserializeOffsetDateTimeSupplier")
public void deserializeJson(String offsetDateTimeString, OffsetDateTime expected) {
assertEquals(expected, DateTimeDeserializer.deserialize(offsetDateTimeString));
}

private static Stream<Arguments> deserializeOffsetDateTimeSupplier() {
OffsetDateTime minValue = OffsetDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
OffsetDateTime unixEpoch = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);

return Stream.of(
Arguments.of("0001-01-01T00:00:00", minValue),
Arguments.of("0001-01-01T00:00:00Z", minValue),
Arguments.of("1970-01-01T00:00:00", unixEpoch),
Arguments.of("1970-01-01T00:00:00Z", unixEpoch)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -79,4 +85,63 @@ public String getValue() {
return value;
}
}

@ParameterizedTest
@MethodSource("deserializeJsonSupplier")
public void deserializeJson(String json, OffsetDateTime expected) throws IOException {
DateTimeWrapper wrapper = JacksonAdapter.createDefaultSerializerAdapter()
.deserialize(json, DateTimeWrapper.class, SerializerEncoding.JSON);

assertEquals(expected, wrapper.getOffsetDateTime());
}

private static Stream<Arguments> deserializeJsonSupplier() {
final String jsonFormat = "{\"OffsetDateTime\":\"%s\"}";
OffsetDateTime minValue = OffsetDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
OffsetDateTime unixEpoch = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);

return Stream.of(
Arguments.of(String.format(jsonFormat, "0001-01-01T00:00:00"), minValue),
Arguments.of(String.format(jsonFormat, "0001-01-01T00:00:00Z"), minValue),
Arguments.of(String.format(jsonFormat, "1970-01-01T00:00:00"), unixEpoch),
Arguments.of(String.format(jsonFormat, "1970-01-01T00:00:00Z"), unixEpoch)
);
}

@ParameterizedTest
@MethodSource("deserializeXmlSupplier")
public void deserializeXml(String xml, OffsetDateTime expected) throws IOException {
DateTimeWrapper wrapper = JacksonAdapter.createDefaultSerializerAdapter()
.deserialize(xml, DateTimeWrapper.class, SerializerEncoding.XML);

assertEquals(expected, wrapper.getOffsetDateTime());
}

private static Stream<Arguments> deserializeXmlSupplier() {
final String xmlFormat = "<Wrapper><OffsetDateTime>%s</OffsetDateTime></Wrapper>";
OffsetDateTime minValue = OffsetDateTime.of(1, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
OffsetDateTime unixEpoch = OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);

return Stream.of(
Arguments.of(String.format(xmlFormat, "0001-01-01T00:00:00"), minValue),
Arguments.of(String.format(xmlFormat, "0001-01-01T00:00:00Z"), minValue),
Arguments.of(String.format(xmlFormat, "1970-01-01T00:00:00"), unixEpoch),
Arguments.of(String.format(xmlFormat, "1970-01-01T00:00:00Z"), unixEpoch)
);
}

@JacksonXmlRootElement(localName = "Wrapper")
private static class DateTimeWrapper {
@JsonProperty(value = "OffsetDateTime", required = true)
private OffsetDateTime offsetDateTime;

public DateTimeWrapper setOffsetDateTime(OffsetDateTime offsetDateTime) {
this.offsetDateTime = offsetDateTime;
return this;
}

public OffsetDateTime getOffsetDateTime() {
return offsetDateTime;
}
}
}