From 8f5b16946f6be6b17035ceb55d2ea143c0252963 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 6 Jun 2020 00:55:11 -0500 Subject: [PATCH 1/3] Do not adjust Time Zone if value is OffsetDateTime.MIN or OffsetDateTime.MAX with ADJUST_DATES_TO_CONTEXT_TIME_ZONE enabled. --- datetime/README.md | 2 +- .../jsr310/deser/InstantDeserializer.java | 2 +- .../jsr310/deser/OffsetDateTimeDeserTest.java | 39 +++++++++++++++++++ release-notes/CREDITS-2.x | 4 ++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/datetime/README.md b/datetime/README.md index 9405d98f..4c8bc5ad 100644 --- a/datetime/README.md +++ b/datetime/README.md @@ -23,7 +23,7 @@ more ambiguous integer types are read as fractional seconds without a decimal po For TimeZone handling, `ADJUST_DATES_TO_CONTEXT_TIME_ZONE` (default: true) specifies whether the context provided by `java.time.TimeZone` 'SerializedProvider#getTimeZone()' should be used to adjust Date/Time values on deserialization, even if the value itself -contains timezone information. If disabled, it will only be used if the value itself does not contain any TimeZone information. +contains timezone information. If the value is `OffsetDateTime.MIN` or `OffsetDateTime.MAX`, the Date/Time value will not be adjusted. If disabled, it will only be used if the value itself does not contain any TimeZone information. Finally, there are two features that apply to array handling. `UNWRAP_SINGLE_VALUE_ARRAYS` (default: false) allows auto-conversion from single-element arrays to non-JSON-array values. If the JSON value contains more than one element in the array, deserialization will still fail. `ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT` (default: false) determines whether empty Array value ("[ ]" in JSON) is accepted diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java index 880f7c39..11c5718f 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java @@ -73,7 +73,7 @@ public class InstantDeserializer OffsetDateTime::from, a -> OffsetDateTime.ofInstant(Instant.ofEpochMilli(a.value), a.zoneId), a -> OffsetDateTime.ofInstant(Instant.ofEpochSecond(a.integer, a.fraction), a.zoneId), - (d, z) -> d.withOffsetSameInstant(z.getRules().getOffset(d.toLocalDateTime())), + (d, z) -> (d.isEqual(OffsetDateTime.MIN) || d.isEqual(OffsetDateTime.MAX) ? d : d.withOffsetSameInstant(z.getRules().getOffset(d.toLocalDateTime()))), true // yes, replace zero offset with Z ); diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetDateTimeDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetDateTimeDeserTest.java index 3752f2ed..d0c9900d 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetDateTimeDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetDateTimeDeserTest.java @@ -657,6 +657,45 @@ public void testStrictDeserializeFromEmptyString() throws Exception { objectReader.readValue(valueFromEmptyStr); } + // [module-java8#166] + @Test + public void testDeserializationNoAdjustIfMIN() throws Exception + { + OffsetDateTime date = OffsetDateTime.MIN; + ObjectMapper m = newMapper() + .configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true) + .setTimeZone(TimeZone.getTimeZone(Z1)) + .addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = m.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + OffsetDateTime actualValue = (OffsetDateTime) value; + assertIsEqual(date, actualValue); + assertEquals(date.getOffset(),actualValue.getOffset()); + } + + @Test + public void testDeserializationNoAdjustIfMAX() throws Exception + { + OffsetDateTime date = OffsetDateTime.MAX; + ObjectMapper m = newMapper() + .configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true) + .setTimeZone(TimeZone.getTimeZone(Z1)) + .addMixIn(Temporal.class, MockObjectConfiguration.class); + Temporal value = m.readValue( + "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class + ); + + assertNotNull("The value should not be null.", value); + assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + OffsetDateTime actualValue = (OffsetDateTime) value; + assertIsEqual(date, actualValue); + assertEquals(date.getOffset(),actualValue.getOffset()); + } + private static void assertIsEqual(OffsetDateTime expected, OffsetDateTime actual) { assertTrue("The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">.", diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 812efb21..09569d3a 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -103,3 +103,7 @@ Arturas Gusevas (agusevas@github) Samantha Williamson (samwill@github) * Contributed fix to #148: Allow strict `LocalDate` parsing (2.11.0) + +Moritz Orth (morth@github) + * Reported and suggested fix for #166: Cannot deserialize OffsetDateTime.MIN and + OffsetDateTime.MAX with ADJUST_DATES_TO_CONTEXT_TIME_ZONE enabled (2.11) From 66bc420dae4aa96b8cd22785c3f011b23bdc6a93 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 6 Jun 2020 01:06:18 -0500 Subject: [PATCH 2/3] deleted private e-mail --- release-notes/CREDITS-2.x | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 09569d3a..2bfe049d 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -104,6 +104,6 @@ Samantha Williamson (samwill@github) * Contributed fix to #148: Allow strict `LocalDate` parsing (2.11.0) -Moritz Orth (morth@github) +Moritz Orth * Reported and suggested fix for #166: Cannot deserialize OffsetDateTime.MIN and OffsetDateTime.MAX with ADJUST_DATES_TO_CONTEXT_TIME_ZONE enabled (2.11) From ba0848517cdea458568202332e02be5ccc0c9936 Mon Sep 17 00:00:00 2001 From: Mike Date: Sat, 6 Jun 2020 02:26:38 -0500 Subject: [PATCH 3/3] Fixing e-mail. --- release-notes/CREDITS-2.x | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 2bfe049d..a218fe5b 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -104,6 +104,6 @@ Samantha Williamson (samwill@github) * Contributed fix to #148: Allow strict `LocalDate` parsing (2.11.0) -Moritz Orth +Moritz Orth (morth@github.com) * Reported and suggested fix for #166: Cannot deserialize OffsetDateTime.MIN and - OffsetDateTime.MAX with ADJUST_DATES_TO_CONTEXT_TIME_ZONE enabled (2.11) + OffsetDateTime.MAX with ADJUST_DATES_TO_CONTEXT_TIME_ZONE enabled (2.12)