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

Deserialization of null value for nullable field fails #53

Closed
conorfarrell-coats opened this issue Feb 24, 2021 · 3 comments
Closed

Deserialization of null value for nullable field fails #53

conorfarrell-coats opened this issue Feb 24, 2021 · 3 comments
Labels
inmaster The issue is fixed in the master branch

Comments

@conorfarrell-coats
Copy link

We've recently updated xmlutil to version 0.81.0, and seem to have come across a bug in how deserialization for nullable fields is handled.

Our application accepts XML from a third party system, which we don't control. At times, rather than omit an element for missing data, it will instead provide XML with an element which is present but not real data, e.g.
<Doc><Date>00.00.0000</Date></Doc> instead of <Doc></Doc>. We handle this by having a Serializer that recognises these values and outputs null instead, which is the semantically correct value.

On the previous xmlutil version we were using (0.13.0.2, so quite old), this worked fine. However, with 0.81.0, deserialization fails with the following error:

java.lang.NullPointerException: null cannot be cast to non-null type T of nl.adaptivity.xmlutil.serialization.XmlDecoderBase.TagDecoder.decodeNullableSerializableElement
	at nl.adaptivity.xmlutil.serialization.XmlDecoderBase$TagDecoder.decodeNullableSerializableElement(XMLDecoder.kt:393)

From looking at the XMLDecoder code, it seems to be casting the result of the operation to T rather than T?, which may be the cause, though I'm not certain.

A small test which reproduces the issue is here: https://gist.github.com/conorfarrell-coats/f6a6970dd74196b3ab47a289a2dc99c9

If we're holding it wrong, very happy to be told this! Also happy to work with you to make a fix if you can give us some guidance.

@pdvrieze pdvrieze added the inmaster The issue is fixed in the master branch label Feb 24, 2021
@pdvrieze
Copy link
Owner

The quick fix is indeed casting to T? rather than to T. I've added your test GIST to the repository (using JUnit though) - thanks for that.
In addition I've made it such that encoding also works "properly". There is one error in the sample code that is significant to that. You need to have a descriptor that marks the serializer as nullable. So the code you need is:

    override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDate:$format", PrimitiveKind.STRING).nullable

note the .nullable at the end.

@conorfarrell-coats
Copy link
Author

Thank you so much for responding to this so quickly, and for the tip re: nullable for the SerialDescriptor! Looking forward to the next release so I can remove my workaround for this issue. :)

@pdvrieze
Copy link
Owner

Now in 0.81.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inmaster The issue is fixed in the master branch
Projects
None yet
Development

No branches or pull requests

2 participants