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

Fix NPE when parsing invalid timestamp variable inputs #518

Merged
merged 1 commit into from
Jan 31, 2025
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
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