Skip to content

Commit 928bfae

Browse files
fix(client): preserve time zone in lenient date-time parsing
1 parent 4a49c05 commit 928bfae

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

orb-java-core/src/main/kotlin/com/withorb/api/core/ObjectMappers.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import java.time.DateTimeException
2525
import java.time.LocalDate
2626
import java.time.LocalDateTime
2727
import java.time.OffsetDateTime
28-
import java.time.ZonedDateTime
28+
import java.time.ZoneId
2929
import java.time.format.DateTimeFormatter
3030
import java.time.temporal.ChronoField
3131

@@ -157,14 +157,15 @@ private class LenientOffsetDateTimeDeserializer :
157157
val temporal = formatter.parse(p.text)
158158

159159
return when {
160-
!temporal.isSupported(ChronoField.HOUR_OF_DAY) ->
161-
LocalDate.from(temporal).atStartOfDay()
162-
!temporal.isSupported(ChronoField.OFFSET_SECONDS) ->
163-
LocalDateTime.from(temporal)
164-
else -> ZonedDateTime.from(temporal).toLocalDateTime()
165-
}
166-
.atZone(context.timeZone.toZoneId())
167-
.toOffsetDateTime()
160+
!temporal.isSupported(ChronoField.HOUR_OF_DAY) ->
161+
LocalDate.from(temporal)
162+
.atStartOfDay()
163+
.atZone(ZoneId.of("UTC"))
164+
.toOffsetDateTime()
165+
!temporal.isSupported(ChronoField.OFFSET_SECONDS) ->
166+
LocalDateTime.from(temporal).atZone(ZoneId.of("UTC")).toOffsetDateTime()
167+
else -> OffsetDateTime.from(temporal)
168+
}
168169
} catch (e: DateTimeException) {
169170
exceptions.add(e)
170171
}

orb-java-core/src/test/kotlin/com/withorb/api/core/ObjectMappersTest.kt

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package com.withorb.api.core
33
import com.fasterxml.jackson.annotation.JsonProperty
44
import com.fasterxml.jackson.databind.exc.MismatchedInputException
55
import com.fasterxml.jackson.module.kotlin.readValue
6+
import java.time.LocalDate
7+
import java.time.LocalTime
68
import java.time.OffsetDateTime
9+
import java.time.ZoneOffset
710
import kotlin.reflect.KClass
811
import org.assertj.core.api.Assertions.assertThat
912
import org.assertj.core.api.Assertions.catchThrowable
1013
import org.junit.jupiter.api.Test
11-
import org.junit.jupiter.api.assertDoesNotThrow
1214
import org.junit.jupiter.params.ParameterizedTest
1315
import org.junit.jupiter.params.provider.EnumSource
1416
import org.junitpioneer.jupiter.cartesian.CartesianTest
@@ -72,11 +74,34 @@ internal class ObjectMappersTest {
7274
}
7375
}
7476

75-
enum class LenientOffsetDateTimeTestCase(val string: String) {
76-
DATE("1998-04-21"),
77-
DATE_TIME("1998-04-21T04:00:00"),
78-
ZONED_DATE_TIME_1("1998-04-21T04:00:00+03:00"),
79-
ZONED_DATE_TIME_2("1998-04-21T04:00:00Z"),
77+
enum class LenientOffsetDateTimeTestCase(
78+
val string: String,
79+
val expectedOffsetDateTime: OffsetDateTime,
80+
) {
81+
DATE(
82+
"1998-04-21",
83+
expectedOffsetDateTime =
84+
OffsetDateTime.of(LocalDate.of(1998, 4, 21), LocalTime.of(0, 0), ZoneOffset.UTC),
85+
),
86+
DATE_TIME(
87+
"1998-04-21T04:00:00",
88+
expectedOffsetDateTime =
89+
OffsetDateTime.of(LocalDate.of(1998, 4, 21), LocalTime.of(4, 0), ZoneOffset.UTC),
90+
),
91+
ZONED_DATE_TIME_1(
92+
"1998-04-21T04:00:00+03:00",
93+
expectedOffsetDateTime =
94+
OffsetDateTime.of(
95+
LocalDate.of(1998, 4, 21),
96+
LocalTime.of(4, 0),
97+
ZoneOffset.ofHours(3),
98+
),
99+
),
100+
ZONED_DATE_TIME_2(
101+
"1998-04-21T04:00:00Z",
102+
expectedOffsetDateTime =
103+
OffsetDateTime.of(LocalDate.of(1998, 4, 21), LocalTime.of(4, 0), ZoneOffset.UTC),
104+
),
80105
}
81106

82107
@ParameterizedTest
@@ -85,6 +110,8 @@ internal class ObjectMappersTest {
85110
val jsonMapper = jsonMapper()
86111
val json = jsonMapper.writeValueAsString(testCase.string)
87112

88-
assertDoesNotThrow { jsonMapper().readValue<OffsetDateTime>(json) }
113+
val offsetDateTime = jsonMapper().readValue<OffsetDateTime>(json)
114+
115+
assertThat(offsetDateTime).isEqualTo(testCase.expectedOffsetDateTime)
89116
}
90117
}

0 commit comments

Comments
 (0)