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 String values in Arrays / Collections not working as expected #584

Closed
mbladel opened this issue Mar 17, 2023 · 3 comments
Labels
2.15 For issues planned for 2.15
Milestone

Comments

@mbladel
Copy link
Contributor

mbladel commented Mar 17, 2023

Environment: Jackson Dataformat XML 2.14.2

We are trying to serialize and deserialize arrays or collections of Strings containing both empty and null values.
We noticed that nulls are handled correctly when serializing:

  • null values are rendered as <item />, while empty strings as <item></item>
  • enabling ToXmlGenerator.Feature.WRITE_NULLS_AS_XSI_NIL works as expected

When parsing this values back null elements have become empty ("") values.
We tried:

  • explicitly enabling FromXmlParser.Feature.PROCESS_XSI_NIL, even though it should be enabled by default
  • enabling FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL
    But we still got empty String values as a result.

Testing showed that with simple String attributes inside a class both null and empty values are correctly handled during deserialization, as expected (see also #354).

The behavior of String values inside collections should be aligned to how String attributes inside objects are handled.

Here's an example:

XmlMapper xmlMapper = new XmlMapper();
xmlMapper.enable( ToXmlGenerator.Feature.WRITE_NULLS_AS_XSI_NIL );
xmlMapper.enable( FromXmlParser.Feature.PROCESS_XSI_NIL );

String string = xmlMapper.writeValueAsString( new String[] { "", "test", null, "test2" } );
// string: <Strings><item></item><item>test</item><item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/><item>test2</item></Strings>
String[] parsed =  xmlMapper.readValue( string, String[].class );
// parsed: ["", "test", "", "test2"]
@cowtowncoder
Copy link
Member

Quick note(s):

<item /> and <item></item> are semantically equivalent in XML and although (some) parsers can indicate difference (Woodstox does) -- and most can produce one or the other explicitly -- it's not a good idea to try to use difference for semantics, due to interoperability concerns.

Having said that, yes, handling of String values in Arrays or typed Collections should work the same way as individual properties. Handling of difference between null and empty String are tricky, and I think Array/Collection deserializers probably have some shortcuts that would need to be disabled for XML.

@cowtowncoder cowtowncoder changed the title Deserialization of null String values in arrays not working as expected Deserialization of null String values in Arrays / Collections (and probably Maps) not working as expected Mar 17, 2023
@mbladel
Copy link
Contributor Author

mbladel commented Mar 17, 2023

Thanks for the feedback.

<item /> and <item></item> are semantically equivalent in XML

I agree that's working as expected, that is why I went for the xsi:nil approach in the example - just wanted to exhaust all possibilities with the different configurations/features and let you know what I found from my testing.

@cowtowncoder
Copy link
Member

@mbladel yes good. I did realize it is/was not the main point. Mostly mentioned since at some point I was trying to consider/expose distinction and spent some time thinking about it.

@cowtowncoder cowtowncoder changed the title Deserialization of null String values in Arrays / Collections (and probably Maps) not working as expected Deserialization of null String values in Arrays / Collections not working as expected Mar 18, 2023
cowtowncoder added a commit that referenced this issue Mar 18, 2023
@cowtowncoder cowtowncoder added this to the 2.15.0-rc1 milestone Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.15 For issues planned for 2.15
Projects
None yet
Development

No branches or pull requests

2 participants