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

NPE when serializing a Timestamp field with custom date-time formatter #577

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

greek1979
Copy link
Contributor

@greek1979 greek1979 commented Sep 5, 2022

While working on an unrelated project, I found an issue in Yasson when trying to serialize a POJO with a SQL Timestamp field with a custom date-time formatter enabled in Yasson configuration. The default formatters works just fine, but any explicitly specified formatter produces an NPE in SqlTimestampSerializer::formatWithFormatter method as it calls AbstractDateSerializer::toTemporalAccessor method which simply casts its argument to a TemporalAccessor. This does not work with legacy java.sql.Timestamp object, however, as Timestamp does not implement a TemporalAccessor read-only interface.

Here is a small patch to the SqlTimestampSerializer class to convert a Timestamp into a LocalDateTime (Java 8 Date API) as both hold the same moment-in-time value, UTC time zone, so no information loss occurs. Resulting temporal field cannot be still serialized to JSON with a custom date-time format that includes unsupported elements such as time zone, for example, as neither SQL Timestamp nor LocaDateTime contain any time zone information. This is expected and is by design (of a rather messy java.sql.Timestamp class).

@greek1979 greek1979 force-pushed the npe-custom-timestamp-formatter branch 2 times, most recently from 5c90000 to fc0a724 Compare September 6, 2022 11:25
@greek1979
Copy link
Contributor Author

Sorry about a tab character that failed the prebuild checks; replaced it with whitespaces now.

@Verdent
Copy link
Member

Verdent commented Sep 6, 2022

Hi @greek1979 , thank you for providing this patch :-) May I ask you please to also include test, so we can prevent regression?

@greek1979
Copy link
Contributor Author

TL;DR: unit test added.

@Verdent , thank you for pointing this out! Writing a simple test case proved to be an enlightening experience, in fact. As I discovered that an Instant is not a good substitute for SQL timestamp; it does not expose any DATE fields. A time zone-agnostic LocalDateTime proved to be a better alternative.

@greek1979 greek1979 force-pushed the npe-custom-timestamp-formatter branch from 5d68bfa to 3e482ef Compare October 4, 2022 10:16
@greek1979 greek1979 force-pushed the npe-custom-timestamp-formatter branch from 12d9fac to dbf3e58 Compare February 17, 2023 10:23
@greek1979
Copy link
Contributor Author

Rebased feature branch from master to fix all the copyright errors that Checkstyle complained about yesterday...

@greek1979 greek1979 force-pushed the npe-custom-timestamp-formatter branch from dbf3e58 to e36b2b1 Compare February 24, 2023 19:03
@greek1979
Copy link
Contributor Author

Updated this old pull request so the copyright year would comply with Checkstyle strict requirements...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants