Discovered this bug while attempting to serialize some JSON with a nullable Date field.
Steps to NPE:
null value into RFC3339DateJsonAdapter.toJson()null value into ISO8601Utils to be formattedformat() method does is creates a Calendar and sets the time with a dateCalendar.setTime(Date) assumes the date is not null because the first thing it calls is date.getTime() which in this case, throws a NPECall .nullSafe() on the adapter.
Any action to take here? Should we document this?
I would have been helped a lot if this were documented with a short example. I will make a PR for the readme to clarify this feature.
For me as a newcomer to Moshi it took me a while to find this issue, but even the comment mentioning .nullSafe() -- which is the solution -- didn't help me, because I simply didn't understand when or where to use it.
I think with null safety being such a paramount feature of the Kotlin language, you should feature how to work with it in the readme.
Not sure if it's reported else where, but moshi codegen doesn't handle this correctly it will generate:
0 -> timestamp = nullableDateAdapter.fromJson(reader)
When it should generate:
0 -> timestamp = nullableDateAdapter.nullSafe().fromJson(reader)
Is there something I'm missing?
Can fix.
It looks like it's an explicit choice to leave null-checking to installed or generated adapters. Maybe the fix here should go in the RFC3339DateJsonAdapter adapter itself, rather than in generated code?
Seems reasonable. @swankjesse?
On Tue, Jul 16, 2019 at 2:59 PM Ben Bader notifications@github.com wrote:
It looks like it's an explicit choice to leave null-checking to installed
or generated adapters. Maybe the fix here should go in the
RFC3339DateJsonAdapter adapter itself, rather than in generated code?—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/moshi/issues/723?email_source=notifications&email_token=AAKMJPVUWQGZF7OFO45RA6LP7YD6ZA5CNFSM4GALSDT2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2BU76Y#issuecomment-511922171,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAKMJPX5WXTQBP3EZ4EXN7DP7YD6ZANCNFSM4GALSDTQ
.
Opened a PR for this in #910