diff --git a/CHANGELOG.md b/CHANGELOG.md index 554b2196..5c33b64f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,11 @@ CHANGELOG response values, for example `business` and `live`, deserialized as `null`. The library now uses `Locale.ROOT` for these case conversions. * Added `RYFT` to the `Payment.Processor` enum. +* A `ZonedDateTime` request value whose UTC offset has non-zero seconds is + now sent with the offset rounded to the nearest minute. The instant does not + change. Previously, the web service ignored these values and returned a + warning. This affects `Event.time`, `phoneVerificationTime` on `Billing` + and `Shipping`, and `Email.verificationTime`. 4.4.0 (2026-07-21) ------------------ diff --git a/src/main/java/com/maxmind/minfraud/Mapper.java b/src/main/java/com/maxmind/minfraud/Mapper.java index d383e646..a4d0157e 100644 --- a/src/main/java/com/maxmind/minfraud/Mapper.java +++ b/src/main/java/com/maxmind/minfraud/Mapper.java @@ -1,17 +1,28 @@ package com.maxmind.minfraud; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.fasterxml.jackson.databind.util.StdDateFormat; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import java.io.IOException; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; class Mapper { private static final ObjectMapper mapper = JsonMapper.builder() .addModule(new JavaTimeModule()) + // Added after JavaTimeModule so that it replaces its ZonedDateTime serializer. + .addModule(new SimpleModule() + .addSerializer(ZonedDateTime.class, new WholeMinuteOffsetSerializer())) .defaultDateFormat(new StdDateFormat().withColonInTimeZone(true)) .enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING) .enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING) @@ -26,4 +37,28 @@ class Mapper { public static ObjectMapper get() { return mapper; } + + /** + * Writes a {@code ZonedDateTime} with {@code ISO_OFFSET_DATE_TIME}. RFC 3339 offsets have no + * seconds, so an offset with seconds is rounded to the nearest minute, as in the last + * example in RFC 3339 section 5.8. The instant does not change. + */ + private static final class WholeMinuteOffsetSerializer + extends StdSerializer { + WholeMinuteOffsetSerializer() { + super(ZonedDateTime.class); + } + + @Override + public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider provider) + throws IOException { + int offsetSeconds = value.getOffset().getTotalSeconds(); + if (offsetSeconds % 60 != 0) { + var minutes = (int) Math.round(offsetSeconds / 60.0); + var offset = ZoneOffset.ofTotalSeconds(minutes * 60); + value = value.withZoneSameInstant(offset); + } + gen.writeString(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(value)); + } + } } diff --git a/src/test/java/com/maxmind/minfraud/MapperTest.java b/src/test/java/com/maxmind/minfraud/MapperTest.java new file mode 100644 index 00000000..28dca0c1 --- /dev/null +++ b/src/test/java/com/maxmind/minfraud/MapperTest.java @@ -0,0 +1,62 @@ +package com.maxmind.minfraud; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class MapperTest { + + static Stream zonedDateTimes() { + return Stream.of( + // Offsets with seconds are rounded to the nearest minute. Math.round rounds + // -44.5 minutes to -44. + Arguments.of( + ZonedDateTime.of(1971, 6, 1, 10, 0, 0, 0, ZoneId.of("Africa/Monrovia")), + "1971-06-01T10:00:30-00:44"), + Arguments.of( + ZonedDateTime.of(2026, 10, 1, 10, 0, 0, 0, + ZoneOffset.ofHoursMinutesSeconds(5, 30, 15)), + "2026-10-01T09:59:45+05:30"), + Arguments.of( + ZonedDateTime.of(2026, 10, 1, 10, 0, 0, 0, + ZoneOffset.ofHoursMinutesSeconds(-5, -30, -15)), + "2026-10-01T10:00:15-05:30"), + Arguments.of( + ZonedDateTime.of(2026, 10, 1, 10, 0, 0, 0, + ZoneOffset.ofHoursMinutesSeconds(5, 30, 45)), + "2026-10-01T10:00:15+05:31"), + Arguments.of( + ZonedDateTime.of(2026, 10, 1, 10, 0, 0, 0, + ZoneOffset.ofHoursMinutesSeconds(-5, -30, -45)), + "2026-10-01T09:59:45-05:31"), + Arguments.of( + ZonedDateTime.of(2026, 1, 1, 0, 0, 0, 0, ZoneOffset.ofTotalSeconds(-29)), + "2026-01-01T00:00:29Z"), + // Other values are unchanged. + Arguments.of( + ZonedDateTime.of(2026, 10, 1, 10, 30, 0, 0, ZoneId.of("America/New_York")), + "2026-10-01T10:30:00-04:00"), + Arguments.of( + ZonedDateTime.of(2026, 10, 1, 10, 30, 0, 123456789, ZoneId.of("Asia/Kolkata")), + "2026-10-01T10:30:00.123456789+05:30"), + Arguments.of( + ZonedDateTime.parse("2026-10-05T23:20:50.52Z"), + "2026-10-05T23:20:50.52Z") + ); + } + + @ParameterizedTest + @MethodSource("zonedDateTimes") + public void testZonedDateTimeSerialization(ZonedDateTime time, String expected) + throws Exception { + var json = Mapper.get().writeValueAsString(time); + assertEquals("\"" + expected + "\"", json); + assertEquals(time.toInstant(), ZonedDateTime.parse(expected).toInstant()); + } +}