Skip to content

Commit bc55e8e

Browse files
authored
Fix NPE when parsing invalid timestamp variable inputs (#518)
1 parent 14c234e commit bc55e8e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

scalars/src/main/java/com/introproventures/graphql/jpa/query/schema/JavaScalars.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,7 @@ private Timestamp doConvert(Object input) {
741741
if (input instanceof Long longInput) {
742742
return new Timestamp(longInput);
743743
} else if (input instanceof String stringInput) {
744-
Instant instant = DateTimeHelper.parseDate(stringInput);
745-
return Timestamp.from(instant);
744+
return Optional.of(stringInput).map(DateTimeHelper::parseDate).map(Timestamp::from).orElse(null);
746745
} else if (input instanceof Timestamp timestampInput) {
747746
return timestampInput;
748747
} else if (input instanceof Date dateInput) {

scalars/src/test/java/com/introproventures/graphql/jpa/query/schema/JavaScalarsTest.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public void string2ZonedDateTime() {
273273

274274
ZonedDateTime resultLDT = (ZonedDateTime) result;
275275

276-
assert resultLDT.getDayOfMonth() == 05;
276+
assert resultLDT.getDayOfMonth() == 5;
277277
assert resultLDT.getMonth() == Month.AUGUST;
278278
assert resultLDT.getYear() == 2019;
279279
assert resultLDT.getHour() == 13;
@@ -365,6 +365,20 @@ public void testTimestampParseDateValue() {
365365
.isEqualTo(expected);
366366
}
367367

368+
@Test
369+
public void testTimestampParseInvalidValue() {
370+
//given
371+
Coercing<?, ?> coercing = JavaScalars.of(Timestamp.class).getCoercing();
372+
373+
//when
374+
var throwable = catchThrowable(() -> coercing.parseValue("foobar"));
375+
376+
//then
377+
assertThat(throwable)
378+
.isInstanceOf(CoercingParseValueException.class)
379+
.hasMessageContaining("Invalid value 'foobar' for Timestamp");
380+
}
381+
368382
@Test
369383
public void testTimestampSerializeDateValue() {
370384
//given

0 commit comments

Comments
 (0)