jackson-dataformat-xml icon indicating copy to clipboard operation
jackson-dataformat-xml copied to clipboard

`XmlMapper`/`UntypedObjectDeserializer` swallows duplicated elements in XML documents

Open joaovarandas opened this issue 10 years ago • 31 comments

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

  • https://gist.github.com/joaovarandas/1543e792ed6204f0cf5fe860cb7d58ed

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

joaovarandas avatar Aug 04 '16 01:08 joaovarandas

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

Let me know if you want me to pull this.

joaovarandas avatar Aug 04 '16 18:08 joaovarandas

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?

cowtowncoder avatar Aug 08 '16 22:08 cowtowncoder

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!

joaovarandas avatar Aug 08 '16 22:08 joaovarandas

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.

cowtowncoder avatar Aug 08 '16 23:08 cowtowncoder

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

joaovarandas avatar Aug 08 '16 23:08 joaovarandas

@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;
}

joaovarandas avatar Aug 09 '16 13:08 joaovarandas

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.

cowtowncoder avatar Aug 16 '16 20:08 cowtowncoder

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

Paradoxia avatar Mar 03 '17 14:03 Paradoxia

@joaovarandas Saved me, too. Thanks.

arakelian avatar Sep 12 '17 17:09 arakelian

@joaovarandas thanks, your gist solution works perfect.

perezmlal avatar Sep 22 '17 08:09 perezmlal

Thanks @joaovarandas for your gist

gonzalogarciajaubert avatar Feb 06 '18 09:02 gonzalogarciajaubert

Thanks @joaovarandas for the solution in gist

isipkh avatar Mar 13 '18 09:03 isipkh

Thanks @joaovarandas for the gist. Worked perfectly.

dadoonet avatar Aug 23 '18 13:08 dadoonet

I linked this gem to a relevant StackOverflow post. Thanks @joaovarandas for the report and the temporary fix!

vy avatar Sep 14 '18 19:09 vy

@cowtowncoder, could you please clarify whether this issue has been solved? I wonder whether there is a way to support XML with duplicate child nodes? In the worst case scenario, is there any way to get all XML child node loaded?

Using version 2.9.4 currently.

Thank you!

Musikolo avatar Nov 16 '18 21:11 Musikolo

@Musikolo it hasn't, and I don't see a way to handle it via Untyped object deserialization. Jackson XML module does not offer general-purpose node approach to XML -- problem is that XML simply has no native difference that would allow separating Arrays (lists) from Objects (maps), and I really really dislike heuristics that try to separate single-value from multiple-values, as this has proven not to be reliable or stable handling for anything.

cowtowncoder avatar Dec 19 '18 00:12 cowtowncoder

Excuse my probably naive question, but why can't we simply use a MultiMap instead of a Map and be good?

BTW: The problem with the approach suggested in this issue is that it does not work if you also need to parse the attributes of the keys / tags with the identical names.

sschuberth avatar Feb 04 '19 12:02 sschuberth

@sschuberth Because "untyped" databinding is defined as using basic Maps, Lists, Numbers, Strings and booleans. It can not be changed to use MultiMaps (and I don't think JDK has MultiMaps at this point, so API shouldn't be tied to specific external library that provides type). Since JSON does not allow multiple values per property it would also not make much sense to complicate handling for data formats other than XML, at least not at this level.

One possibility, I think, could be to make XMLMapper expose different type of tree model. But I am not sure how valuable that would be over someone just using DOM values.

cowtowncoder avatar Feb 06 '19 05:02 cowtowncoder

Absolute life saver!

In order to throw my 5 cents on the topic, for me it was crucial to readValue to Object.class instead of TypeReference of Map<String, Object>.

Thanks for the workaround provided!

goodlifeinc avatar Apr 08 '19 16:04 goodlifeinc

Is there a workaround for the (XML) JsonNode way as well?

ST-DDT avatar Apr 10 '19 15:04 ST-DDT