Skip to content

Commit

Permalink
Fix NPE when parsing invalid timestamp variable inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
igdianov committed Jan 31, 2025
1 parent 14c234e commit c7d9ae6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,7 @@ private Timestamp doConvert(Object input) {
if (input instanceof Long longInput) {
return new Timestamp(longInput);
} else if (input instanceof String stringInput) {
Instant instant = DateTimeHelper.parseDate(stringInput);
return Timestamp.from(instant);
return Optional.of(stringInput).map(DateTimeHelper::parseDate).map(Timestamp::from).orElse(null);
} else if (input instanceof Timestamp timestampInput) {
return timestampInput;
} else if (input instanceof Date dateInput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void string2ZonedDateTime() {

ZonedDateTime resultLDT = (ZonedDateTime) result;

assert resultLDT.getDayOfMonth() == 05;
assert resultLDT.getDayOfMonth() == 5;
assert resultLDT.getMonth() == Month.AUGUST;
assert resultLDT.getYear() == 2019;
assert resultLDT.getHour() == 13;
Expand Down Expand Up @@ -365,6 +365,20 @@ public void testTimestampParseDateValue() {
.isEqualTo(expected);
}

@Test
public void testTimestampParseInvalidValue() {
//given
Coercing<?, ?> coercing = JavaScalars.of(Timestamp.class).getCoercing();

//when
var throwable = catchThrowable(() -> coercing.parseValue("foobar"));

//then
assertThat(throwable)
.isInstanceOf(CoercingParseValueException.class)
.hasMessageContaining("Invalid value 'foobar' for Timestamp");
}

@Test
public void testTimestampSerializeDateValue() {
//given
Expand Down

0 comments on commit c7d9ae6

Please sign in to comment.