Skip to content

Commit

Permalink
Add specifiedByURL and do not permit negative 0 offset
Browse files Browse the repository at this point in the history
  • Loading branch information
dondonz committed Nov 24, 2022
1 parent 17e7881 commit ad7b869
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/graphql/scalars/datetime/DateTimeScalar.java
Expand Up @@ -22,6 +22,7 @@
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.OFFSET_SECONDS;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;

/**
Expand Down Expand Up @@ -95,7 +96,11 @@ public Value<?> valueToLiteral(Object input) {

private OffsetDateTime parseOffsetDateTime(String s, Function<String, RuntimeException> exceptionMaker) {
try {
return OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
OffsetDateTime parse = OffsetDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
if (parse.get(OFFSET_SECONDS) == 0 && s.endsWith("-00:00")) {
throw exceptionMaker.apply("Invalid value : '" + s + "'. Negative zero offset is not allowed");
}
return parse;
} catch (DateTimeParseException e) {
throw exceptionMaker.apply("Invalid RFC3339 value : '" + s + "'. because of : '" + e.getMessage() + "'");
}
Expand All @@ -104,7 +109,8 @@ private OffsetDateTime parseOffsetDateTime(String s, Function<String, RuntimeExc

INSTANCE = GraphQLScalarType.newScalar()
.name("DateTime")
.description("An RFC-3339 compliant DateTime Scalar")
.description("A slightly refined version of RFC-3339 compliant DateTime Scalar")
.specifiedByUrl("https://scalars.graphql.org/andimarek/date-time") // TODO: Change to .specifiedByURL after release of graphql-java v20
.coercing(coercing)
.build();
}
Expand All @@ -124,5 +130,4 @@ private static DateTimeFormatter getCustomDateTimeFormatter() {
.toFormatter();
}


}
18 changes: 18 additions & 0 deletions src/test/groovy/graphql/scalars/datetime/DateTimeScalarTest.groovy
Expand Up @@ -2,6 +2,7 @@ package graphql.scalars.datetime

import graphql.language.StringValue
import graphql.scalars.ExtendedScalars
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingSerializeException
import spock.lang.Specification
Expand All @@ -28,6 +29,7 @@ class DateTimeScalarTest extends Specification {
"1985-04-12T23:20:50.52Z" | mkOffsetDT("1985-04-12T23:20:50.52Z")
"1996-12-19T16:39:57-08:00" | mkOffsetDT("1996-12-19T16:39:57-08:00")
"1937-01-01T12:00:27.87+00:20" | mkOffsetDT("1937-01-01T12:00:27.87+00:20")
"2022-11-24T01:00:01.02+00:00" | mkOffsetDT("2022-11-24T01:00:01.02+00:00")
mkOffsetDT(year: 1980, hour: 3) | mkOffsetDT("1980-08-08T03:10:09+10:00")
mkZonedDT(year: 1980, hour: 3) | mkOffsetDT("1980-08-08T03:10:09+10:00")
}
Expand All @@ -45,6 +47,7 @@ class DateTimeScalarTest extends Specification {
"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")
"2022-11-24T01:00:01.02+00:00" | mkStringValue("2022-11-24T01:00:01.020Z")
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")
}
Expand All @@ -59,6 +62,7 @@ class DateTimeScalarTest extends Specification {
where:
input | expectedValue
"1985-04-12" | CoercingParseValueException
"2022-11-24T01:00:01.02-00:00" | CoercingParseValueException
mkLocalDT(year: 1980, hour: 3) | CoercingParseValueException
666 || CoercingParseValueException
}
Expand All @@ -85,6 +89,7 @@ class DateTimeScalarTest extends Specification {
"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"
"2022-11-24T01:00:01.02+00:00" | "2022-11-24T01:00:01.020Z"
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"
}
Expand All @@ -98,8 +103,21 @@ class DateTimeScalarTest extends Specification {
where:
input | expectedValue
"1985-04-12" | CoercingSerializeException
"2022-11-24T01:00:01.02-00:00" | CoercingSerializeException
mkLocalDT(year: 1980, hour: 3) | CoercingSerializeException
666 || CoercingSerializeException
}

@Unroll
def "datetime parseLiteral bad inputs"() {

when:
coercing.parseLiteral(input)
then:
thrown(expectedValue)
where:
input | expectedValue
"2022-11-24T01:00:01.02-00:00" | CoercingParseLiteralException
}

}

0 comments on commit ad7b869

Please sign in to comment.