Skip to content

Commit b031218

Browse files
fix: add support for deserializing zoned expressions
1 parent beeb208 commit b031218

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

hypertrace-core-graphql-common-schema/src/main/java/org/hypertrace/core/graphql/common/schema/typefunctions/DateTimeScalar.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.lang.reflect.AnnotatedType;
1313
import java.time.DateTimeException;
1414
import java.time.Instant;
15+
import java.time.OffsetDateTime;
1516
import java.time.temporal.TemporalAccessor;
1617
import java.util.function.Function;
1718

@@ -45,10 +46,10 @@ private <E extends GraphqlErrorException> Instant toInstant(
4546
return Instant.from((TemporalAccessor) instantInput);
4647
}
4748
if (instantInput instanceof CharSequence) {
48-
return Instant.parse((CharSequence) instantInput);
49+
return parse((CharSequence) instantInput);
4950
}
5051
if (instantInput instanceof StringValue) {
51-
return Instant.parse(((StringValue) instantInput).getValue());
52+
return parse(((StringValue) instantInput).getValue());
5253
}
5354
} catch (DateTimeException exception) {
5455
throw errorWrapper.apply(exception);
@@ -75,4 +76,8 @@ public GraphQLScalarType buildType(
7576
ProcessingElementsContainer container) {
7677
return DATE_TIME_SCALAR;
7778
}
79+
80+
private static Instant parse(CharSequence charSequence) {
81+
return OffsetDateTime.parse(charSequence).toInstant();
82+
}
7883
}

hypertrace-core-graphql-common-schema/src/test/java/org/hypertrace/core/graphql/common/schema/scalars/DateTimeScalarTest.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
@ExtendWith(MockitoExtension.class)
2020
class DateTimeScalarTest {
2121

22-
private static final String TEST_DATE_TIME_STRING = "2019-10-29T21:30:12.871Z";
23-
private static final Instant TEST_DATE_TIME_INSTANT = Instant.parse(TEST_DATE_TIME_STRING);
22+
private static final String TEST_UTC_DATE_TIME_STRING = "2019-10-29T21:30:12.871Z";
23+
private static final String TEST_ZONED_DATE_TIME_STRING = "2019-10-30T03:00:12.871+05:30";
24+
private static final Instant TEST_DATE_TIME_INSTANT = Instant.parse(TEST_UTC_DATE_TIME_STRING);
2425
private DateTimeScalar dateTimeFunction;
2526
private GraphQLScalarType dateTimeType;
2627
@Mock AnnotatedType mockAnnotatedType;
@@ -45,18 +46,24 @@ void canDetermineIfConvertible() {
4546
@Test
4647
void canConvertFromLiteral() {
4748
assertEquals(
48-
TEST_DATE_TIME_INSTANT, dateTimeType.getCoercing().parseLiteral(TEST_DATE_TIME_STRING));
49+
TEST_DATE_TIME_INSTANT, dateTimeType.getCoercing().parseLiteral(TEST_UTC_DATE_TIME_STRING));
50+
assertEquals(
51+
TEST_DATE_TIME_INSTANT,
52+
dateTimeType.getCoercing().parseLiteral(TEST_ZONED_DATE_TIME_STRING));
4953
}
5054

5155
@Test
5256
void canSerialize() {
5357
assertEquals(
54-
TEST_DATE_TIME_STRING, dateTimeType.getCoercing().serialize(TEST_DATE_TIME_INSTANT));
58+
TEST_UTC_DATE_TIME_STRING, dateTimeType.getCoercing().serialize(TEST_DATE_TIME_INSTANT));
5559
assertEquals(
56-
TEST_DATE_TIME_STRING, dateTimeType.getCoercing().serialize(TEST_DATE_TIME_STRING));
60+
TEST_UTC_DATE_TIME_STRING, dateTimeType.getCoercing().serialize(TEST_UTC_DATE_TIME_STRING));
61+
assertEquals(
62+
TEST_UTC_DATE_TIME_STRING,
63+
dateTimeType.getCoercing().serialize(TEST_ZONED_DATE_TIME_STRING));
5764

5865
assertEquals(
59-
TEST_DATE_TIME_STRING,
66+
TEST_UTC_DATE_TIME_STRING,
6067
dateTimeType
6168
.getCoercing()
6269
.serialize(TEST_DATE_TIME_INSTANT.atOffset(ZoneOffset.ofHoursMinutes(12, 30))));
@@ -68,6 +75,11 @@ void canConvertFromValue() {
6875
TEST_DATE_TIME_INSTANT,
6976
dateTimeType
7077
.getCoercing()
71-
.parseValue(StringValue.newStringValue().value(TEST_DATE_TIME_STRING).build()));
78+
.parseValue(StringValue.newStringValue().value(TEST_UTC_DATE_TIME_STRING).build()));
79+
assertEquals(
80+
TEST_DATE_TIME_INSTANT,
81+
dateTimeType
82+
.getCoercing()
83+
.parseValue(StringValue.newStringValue().value(TEST_ZONED_DATE_TIME_STRING).build()));
7284
}
7385
}

0 commit comments

Comments
 (0)