-
Notifications
You must be signed in to change notification settings - Fork 760
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
Honor Kotlin non-null properties #331
Comments
Remark for others having this problem: if your desired behaviour is to filter null values from your result list, you can write an Adapter wich parses the json into an intermediate data class which can contain nulls. This intermediate data class can be parsed into the desired result and while parsing, you filter all nulls with For my example the Adapter looks like this:
|
For what it's worth, I think the most maintainable approach is to validate objects returned from Moshi manually, and take appropriate action there. Your code will be simpler if you refuse unsupported values rather than patching them, but I have no idea what your system looks like. |
Thanks for the remarks, sounds reasonable. However it should still be considered a bug, that a list with non-nullable values ( |
Good point. Lemme rename the issue and we’ll contemplate what our options are. |
If we wan't to fail early may be it makes sense to add a rule mechanism to the |
Started looking into it, and immediately stumbled upon the fact that any extra adapter for collections will add a side effect for java classes. The only idea that comes to mind at the moment is to tie through an internal api a nullability flag to the generic type for the list/set/map, but that is too smelly. |
Maybe something like .nonNull() on adapters that should never return null. |
It should just throw. It should never initialize non-nullable fields with nulls. |
I'm agreed with @rongi , how to make moshi throw execption now? I have tried but failed. |
@williamwue It throws now, you just need to use |
@rongi Got it, I have miss |
Folding into this issues: #634 |
When I deserialize a json array containing a null into a kotlin list which should not accept null values, I end up with a list which contains a null.
json
kotlin data class
data class VideoPage(val data: List<Video> = listOf())
I am not sure if this is considered a bug and if throwing an exception would be a good solution.
For my use case filtering null values would be the best fit.
The text was updated successfully, but these errors were encountered: