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

DateTime serialization has non optional 3 digit millisecond component #59

Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 24 additions & 1 deletion src/main/java/graphql/scalars/datetime/DateTimeScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.util.function.Function;

import static graphql.scalars.util.Kit.typeName;
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
import static java.time.temporal.ChronoField.HOUR_OF_DAY;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;

/**
* Access this via {@link graphql.scalars.ExtendedScalars#DateTime}
Expand All @@ -27,6 +33,7 @@ public final class DateTimeScalar {
public static final GraphQLScalarType INSTANCE;

private DateTimeScalar() {}
private static final DateTimeFormatter customOutputFormatter = getCustomDateTimeFormatter();

static {
Coercing<OffsetDateTime, String> coercing = new Coercing<OffsetDateTime, String>() {
Expand All @@ -45,7 +52,7 @@ public String serialize(Object input) throws CoercingSerializeException {
);
}
try {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime);
return customOutputFormatter.format(offsetDateTime);
} catch (DateTimeException e) {
throw new CoercingSerializeException(
"Unable to turn TemporalAccessor into OffsetDateTime because of : '" + e.getMessage() + "'."
Expand Down Expand Up @@ -102,4 +109,20 @@ private OffsetDateTime parseOffsetDateTime(String s, Function<String, RuntimeExc
.build();
}

private static DateTimeFormatter getCustomDateTimeFormatter() {
return new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(ISO_LOCAL_DATE)
.appendLiteral('T')
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.appendFraction(NANO_OF_SECOND, 3, 3, true)
.appendOffset("+HH:MM", "Z")
.toFormatter();
}


}
21 changes: 11 additions & 10 deletions src/test/groovy/graphql/scalars/datetime/DateTimeScalarTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ class DateTimeScalarTest extends Specification {
result.isEqualTo(expectedValue)
where:
input | expectedValue
"1985-04-12T23:20:50.52Z" | mkStringValue("1985-04-12T23:20:50.52Z")
"1996-12-19T16:39:57-08:00" | mkStringValue("1996-12-19T16:39:57-08:00")
"1937-01-01T12:00:27.87+00:20" | mkStringValue("1937-01-01T12:00:27.87+00:20")
mkOffsetDT(year: 1980, hour: 3) | mkStringValue("1980-08-08T03:10:09+10:00")
mkZonedDT(year: 1980, hour: 3) | mkStringValue("1980-08-08T03:10:09+10:00")
"1985-04-12T23:20:50.52Z" | mkStringValue("1985-04-12T23:20:50.520Z")
"1996-12-19T16:39:57-08:00" | mkStringValue("1996-12-19T16:39:57.000-08:00")
"1937-01-01T12:00:27.87+00:20" | mkStringValue("1937-01-01T12:00:27.870+00:20")
"1937-01-01T12:00+00:20" | mkStringValue("1937-01-01T12:00:00.000+00:20")
mkOffsetDT(year: 1980, hour: 3) | mkStringValue("1980-08-08T03:10:09.000+10:00")
mkZonedDT(year: 1980, hour: 3) | mkStringValue("1980-08-08T03:10:09.000+10:00")
}

@Unroll
Expand Down Expand Up @@ -81,11 +82,11 @@ class DateTimeScalarTest extends Specification {
result == expectedValue
where:
input | expectedValue
"1985-04-12T23:20:50.52Z" | "1985-04-12T23:20:50.52Z"
"1996-12-19T16:39:57-08:00" | "1996-12-19T16:39:57-08:00"
"1937-01-01T12:00:27.87+00:20" | "1937-01-01T12:00:27.87+00:20"
mkOffsetDT(year: 1980, hour: 3) | "1980-08-08T03:10:09+10:00"
mkZonedDT(year: 1980, hour: 3) | "1980-08-08T03:10:09+10:00"
"1985-04-12T23:20:50.52Z" | "1985-04-12T23:20:50.520Z"
"1996-12-19T16:39:57-08:00" | "1996-12-19T16:39:57.000-08:00"
"1937-01-01T12:00:27.87+00:20" | "1937-01-01T12:00:27.870+00:20"
mkOffsetDT(year: 1980, hour: 3) | "1980-08-08T03:10:09.000+10:00"
mkZonedDT(year: 1980, hour: 3) | "1980-08-08T03:10:09.000+10:00"
}

def "datetime serialisation bad inputs"() {
Expand Down