You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
XmlMapper/UntypedObjectDeserializer swallows duplicated elements in XML documents #205
The gist implements the solution using an extended version of the UntypedObjectDeserializer.
If not the default behavior, what about creating a new DeserializationFeature to enable this(default or not)?
Should you guys like/aprove this solution, I can always fork the project and submit a pull request with the full solution as a feature or default behavior.
Thanks!
Jp
Reacted by Luis Alfredo Perez Medina, David Pilato, Volkan Yazıcı, Sebastian Schuberth, Burkhard Vogel-Kreykenbohm, yididiyan and Kurt Aaholst
changed the title [-]XmlMapper swallows duplicated elements in UntypedObjectDeserializer[/-][+]XmlMapper/UntypedObjectDeserializer swallows duplicated elements in XML documents[/+]on Aug 4, 2016
I don't think this should be the default behavior, but I would be fine with a feature that allows doing this.
One question here is regarding how it should be enabled; at least two possibilities:
Straight setter/getter in XmlMapper (similar to setDefaultUseWrapper)
FromXmlParser.Feature
Of these I would otherwise favor (2) since it's easier to expand, but one problem is that this feature can not be changed on per-call basis and as such should not be a (format-specific) DeserializationFeature. So perhaps (1) is the better way to go.
Alternatively perhaps it would be better to add option (3); add XmlMapper.Feature.
So if you can do a PR for feature itself (with simple setter, for now), and I can retrofit XmlMapper.Feature?
The problem with adding as a "optional" feature (or setter) is that "unaware" developers could lose data deserialized from XML. That's what happened to me: - Users found out the bug weeks after it has been deployed to production, since during the tests there were no repeated elements.
Anyway, sure I can do that. And it fits my use case as well.
Thanks!
Another option would be to explicitly fail on any attempt to bind to "untyped" (java.lang.Object), to make it explicitly clear that failure occurs. I am open to doing this, although that would have to go in next minor version. It would probably also have to apply to Map...
In the end I do not think there are good options: ideally XML is not being mapped to either Object or Map. Trying to auto-detect between List of things and single thing does not work well with Java style typing -- access to such values is awkward. In fact, I might go as far as claiming that it might make more sense to use DOM (and alternatives, JDOM, DOM4J) instead of (Jackson) databind.
I'm currently building a facade for the Apache Components/HTTPClient, and invoking SOAP services... Then I just "transform" the return object to a simple Map/List, and invoke JavaScript code (using Nashorn).
In that use case, it doesn't matter if the object is an Array or a Map since I access them using simple JavaScript bindings like: data["attribute-key"] or data[0] for an array ...
Probably for Scala or other languages inside JVM it would be the same.
I think failing is good too, to fail when there are duplicated elements in a default UntypedObject deser ...
There is no way to deregister a module, since registration really is just a callback to get a chance to register other things. Further, there is no metadata on what registered any given handler (no back-references to module). So answer is you can not. You can just register more handlers.
So question would rather become "what are you trying to do". It is possible to add suitable Serializers / Deserializers handler to -- for example -- avoid return custom serializer/deserializer depending configuration.
One update: created FasterXML/jackson-databind#2732 to help with JsonNode to -- I hope -- essentially implement "coerce as Array if you see "duplicate" property.
This could also pave the way to address "untyped" deserialization case.
changed the title [-]XmlMapper/UntypedObjectDeserializer swallows duplicated elements in XML documents[/-][+]`XmlMapper`/`UntypedObjectDeserializer` swallows duplicated elements in XML documents[/+]on May 22, 2020
I ended up implementing suggestion, so that repeated elements result in automatic "upgrade" into List. This is done automatically when reading xml content.
Will be in 2.12(.0).
Reacted by ST-DDT, Luis Alfredo Perez Medina, Cuneytt, zzk94, Oleg Tymko, David Linsin and Ahmed Ezzat
@taojoe not quite that fast: I am hoping to get the first release candidate (rc1) out within a week, and then bit of time is needed for testing. So earliest I think would be in maybe 4 weeks.
@cowtowncoder rc1 is ok for me. Thanks a lot. ' repeated elements result in automatic "upgrade" into List' is the key part in my project. It'll save a lot of time.
I added a ticket to improve handling of repeated elements, see [https://github.com//issues/495]
For those with Perl background: current solution is similar to XML::Simple forceArray = true, the addition will follow the forceArray = [ tag1, tag2, …] approach. For parsing in Perl, it was a life saver.
Activity
[-]XmlMapper swallows duplicated elements in UntypedObjectDeserializer[/-][+]XmlMapper/UntypedObjectDeserializer swallows duplicated elements in XML documents[/+]fix issue FasterXML#205 - do not swallow duplicated elements in xml>o…
joaovarandas commentedon Aug 4, 2016
The new test 'testDuplicatedElementsSwallowing' in class 'XmlParserTest' ensures it is working as proposed.
Let me know if you want me to pull this.
cowtowncoder commentedon Aug 8, 2016
I don't think this should be the default behavior, but I would be fine with a feature that allows doing this.
One question here is regarding how it should be enabled; at least two possibilities:
XmlMapper(similar tosetDefaultUseWrapper)FromXmlParser.FeatureOf these I would otherwise favor (2) since it's easier to expand, but one problem is that this feature can not be changed on per-call basis and as such should not be a (format-specific)
DeserializationFeature. So perhaps (1) is the better way to go.Alternatively perhaps it would be better to add option (3); add
XmlMapper.Feature.So if you can do a PR for feature itself (with simple setter, for now), and I can retrofit
XmlMapper.Feature?joaovarandas commentedon Aug 8, 2016
The problem with adding as a "optional" feature (or setter) is that "unaware" developers could lose data deserialized from XML. That's what happened to me: - Users found out the bug weeks after it has been deployed to production, since during the tests there were no repeated elements.
Anyway, sure I can do that. And it fits my use case as well.
Thanks!
cowtowncoder commentedon Aug 8, 2016
Another option would be to explicitly fail on any attempt to bind to "untyped" (
java.lang.Object), to make it explicitly clear that failure occurs. I am open to doing this, although that would have to go in next minor version. It would probably also have to apply toMap...In the end I do not think there are good options: ideally XML is not being mapped to either
ObjectorMap. Trying to auto-detect betweenListof things and single thing does not work well with Java style typing -- access to such values is awkward. In fact, I might go as far as claiming that it might make more sense to use DOM (and alternatives, JDOM, DOM4J) instead of (Jackson) databind.joaovarandas commentedon Aug 8, 2016
Yeah... I agree with you, but:
I'm currently building a facade for the Apache Components/HTTPClient, and invoking SOAP services... Then I just "transform" the return object to a simple Map/List, and invoke JavaScript code (using Nashorn).
In that use case, it doesn't matter if the object is an Array or a Map since I access them using simple JavaScript bindings like: data["attribute-key"] or data[0] for an array ...
Probably for Scala or other languages inside JVM it would be the same.
I think failing is good too, to fail when there are duplicated elements in a default UntypedObject deser ...
joaovarandas commentedon Aug 9, 2016
@cowtowncoder how could I "deregister" a Module or the deserializer?
See the code below:
issue FasterXML#205 - add the xml untyped deser as an option
cowtowncoder commentedon Aug 16, 2016
There is no way to deregister a module, since registration really is just a callback to get a chance to register other things. Further, there is no metadata on what registered any given handler (no back-references to module). So answer is you can not. You can just register more handlers.
So question would rather become "what are you trying to do". It is possible to add suitable
Serializers/Deserializershandler to -- for example -- avoid return custom serializer/deserializer depending configuration.Paradoxia commentedon Mar 3, 2017
@joaovarandas Thank you, your gist solution saved my day.
arakelian commentedon Sep 12, 2017
@joaovarandas Saved me, too. Thanks.
perezmlal commentedon Sep 22, 2017
@joaovarandas thanks, your gist solution works perfect.
gonzalogarciajaubert commentedon Feb 6, 2018
Thanks @joaovarandas for your gist
isipkh commentedon Mar 13, 2018
Thanks @joaovarandas for the solution in gist
10 remaining items
cowtowncoder commentedon May 20, 2020
One update: created FasterXML/jackson-databind#2732 to help with
JsonNodeto -- I hope -- essentially implement "coerce as Array if you see "duplicate" property.This could also pave the way to address "untyped" deserialization case.
[-]XmlMapper/UntypedObjectDeserializer swallows duplicated elements in XML documents[/-][+]`XmlMapper`/`UntypedObjectDeserializer` swallows duplicated elements in XML documents[/+]Fix #205 (actually, add unit test; fix itself from databind/2733)
cowtowncoder commentedon May 23, 2020
I ended up implementing suggestion, so that repeated elements result in automatic "upgrade" into
List. This is done automatically when reading xml content.Will be in 2.12(.0).
Fix FasterXML#205 (actually, add unit test; fix itself from databind/…
taojoe commentedon Sep 28, 2020
@cowtowncoder does 2.12 release in a few days?
cowtowncoder commentedon Sep 28, 2020
@taojoe not quite that fast: I am hoping to get the first release candidate (rc1) out within a week, and then bit of time is needed for testing. So earliest I think would be in maybe 4 weeks.
taojoe commentedon Sep 29, 2020
@cowtowncoder rc1 is ok for me. Thanks a lot. ' repeated elements result in automatic "upgrade" into List' is the key part in my project. It'll save a lot of time.
taojoe commentedon Oct 7, 2020
@cowtowncoder when will rc1 release ?
cowtowncoder commentedon Oct 7, 2020
Missed doing it last weekend, now hoping to have time over upcoming weekend, in 4-5 days.
taojoe commentedon Oct 8, 2020
@cowtowncoder thanks
yairlenga commentedon Oct 5, 2021
I added a ticket to improve handling of repeated elements, see [https://github.com//issues/495]
For those with Perl background: current solution is similar to XML::Simple forceArray = true, the addition will follow the forceArray = [ tag1, tag2, …] approach. For parsing in Perl, it was a life saver.