Skip to content

XmlMapper/UntypedObjectDeserializer swallows duplicated elements in XML documents #205

Closed

Description

@joaovarandas

Hello guys.

I think I have already seen this issue around, but, for UntypedObjectDeserializer the solution maybe a bit different.

Entity

<?xml version="1.0" encoding="UTF-8"?>
<person>
   <name>John</name>
   <parent>Jose</parent>
   <parent>Maria</parent>
   <dogs>
      <count>3</count>
      <dog>
         <name>Spike</name>
         <age>12</age>
      </dog>
      <dog>
         <name>Brutus</name>
         <age>9</age>
      </dog>
      <dog>
         <name>Bob</name>
         <age>14</age>
      </dog>
   </dogs>
</person>

Code

new XmlMapper().readValue(xml, Object.class);       

Output

{
  "name" : "John",
  "parent" : "Maria",
  "dogs" : {
    "count" : "3",
    "dog" : {
      "name" : "Bob",
      "age" : "14"
    }
  }
}

Problem

Duplicated elements in the entity get swallowed by current UntypedObjectDeserializer implementation.

I can't use Typed Objects. In my use case, I don't have any typed objects, because I don't know how objects are sent to me.

Possible Solution

While creating the Map for the data, check if there are duplicated keys, and start an Array, with this approach, the output would be:

{
  "name" : "John",
  "parent" : [ "Jose", "Maria" ],
  "dogs" : {
    "count" : "3",
    "dog" : [ {
      "name" : "Spike",
      "age" : "12"
    }, {
      "name" : "Brutus",
      "age" : "9"
    }, {
      "name" : "Bob",
      "age" : "14"
    } ]
  }
}

How to Reproduce

Gist

Artifacts:

  • jackson-core
  • jackson-databind
  • jackson-dataformat-xml

Version:

  • 2.7.4

Conclusion

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

Activity

  1. changed the title [-]XmlMapper swallows duplicated elements in UntypedObjectDeserializer[/-] [+]XmlMapper/UntypedObjectDeserializer swallows duplicated elements in XML documents[/+] on Aug 4, 2016
  2. added a commit that references this issue on Aug 4, 2016
  3. joaovarandas commented on Aug 4, 2016

    @joaovarandas
    Author

    The new test 'testDuplicatedElementsSwallowing' in class 'XmlParserTest' ensures it is working as proposed.

    Let me know if you want me to pull this.

  4. cowtowncoder commented on Aug 8, 2016

    @cowtowncoder
    Member

    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:

    1. Straight setter/getter in XmlMapper (similar to setDefaultUseWrapper)
    2. 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?

  5. joaovarandas commented on Aug 8, 2016

    @joaovarandas
    Author

    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!

  6. cowtowncoder commented on Aug 8, 2016

    @cowtowncoder
    Member

    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.

  7. joaovarandas commented on Aug 8, 2016

    @joaovarandas
    Author

    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 ...

    It's not final yet, but if you wanna take a look at the project:
    https://github.com/inpaas/inpaas-httpclient

  8. joaovarandas commented on Aug 9, 2016

    @joaovarandas
    Author

    @cowtowncoder how could I "deregister" a Module or the deserializer?

    See the code below:

    public XmlMapper setUseXmlUntypedObjectDeserModule(boolean state) {
        if (state == true) {
            Module xmlUntypedObjectDeserModule = new SimpleModule().addDeserializer(Object.class, new XmlUntypedObjectDeserializer());
            registerModule(xmlUntypedObjectDeserModule);
        } else {
            // is there anyway to rollback the state?
        }
    
        return this;
    }
  9. added a commit that references this issue on Aug 9, 2016
  10. cowtowncoder commented on Aug 16, 2016

    @cowtowncoder
    Member

    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.

  11. Paradoxia commented on Mar 3, 2017

    @Paradoxia

    @joaovarandas Thank you, your gist solution saved my day.

  12. arakelian commented on Sep 12, 2017

    @arakelian

    @joaovarandas Saved me, too. Thanks.

  13. perezmlal commented on Sep 22, 2017

    @perezmlal

    @joaovarandas thanks, your gist solution works perfect.

  14. gonzalogarciajaubert commented on Feb 6, 2018

    @gonzalogarciajaubert

    Thanks @joaovarandas for your gist

  15. isipkh commented on Mar 13, 2018

    @isipkh

    Thanks @joaovarandas for the solution in gist

  16. 10 remaining items

  17. cowtowncoder commented on May 20, 2020

    @cowtowncoder
    Member

    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.

  18. added this to the 2.12.0 milestone on May 22, 2020
  19. changed the title [-]XmlMapper/UntypedObjectDeserializer swallows duplicated elements in XML documents[/-] [+]`XmlMapper`/`UntypedObjectDeserializer` swallows duplicated elements in XML documents[/+] on May 22, 2020
  20. added a commit that references this issue on May 22, 2020
    ea65c36
  21. cowtowncoder commented on May 23, 2020

    @cowtowncoder
    Member

    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).

  22. added a commit that references this issue on Sep 4, 2020
    2b9d930
  23. taojoe commented on Sep 28, 2020

    @taojoe

    @cowtowncoder does 2.12 release in a few days?

  24. cowtowncoder commented on Sep 28, 2020

    @cowtowncoder
    Member

    @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.

  25. taojoe commented on Sep 29, 2020

    @taojoe

    @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.

  26. taojoe commented on Oct 7, 2020

    @taojoe

    @cowtowncoder when will rc1 release ?

  27. cowtowncoder commented on Oct 7, 2020

    @cowtowncoder
    Member

    Missed doing it last weekend, now hoping to have time over upcoming weekend, in 4-5 days.

  28. taojoe commented on Oct 8, 2020

    @taojoe
  29. yairlenga commented on Oct 5, 2021

    @yairlenga

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions