FasterXML / jackson-dataformat-xml

Extension for Jackson JSON processor that adds support for serializing POJOs as XML (and deserializing from XML) as an alternative to JSON
Apache License 2.0
561 stars 221 forks source link

Create reproducer test case for #509 #604

Closed ptziegler closed 8 months ago

ptziegler commented 11 months ago

This adds two additional test cases to reproduce #509. Those tests attempt to deserialize the two classes Data and MetaData.

The MetaData class simply contains a list of Data objects. The Data class contains a "key", which is derived from the attribute of the XML node, and a "content" list, which is the arbitrary data stored inside that node.

ptziegler commented 11 months ago

Test Result:

[ERROR] Failures: 
[ERROR]   UnexpectedNonWhitespaceText509Test.testDeSerData:81->deSer:106 Unrecognized field "" (class com.fasterxml.jackson.dataformat.xml.deser.UnexpectedNonWhitespaceText509Test$Data), not marked as ignorable (2 known properties: "content", "key"])
 at [Source: (StringReader); line: 1, column: 40] (through reference chain: com.fasterxml.jackson.dataformat.xml.deser.UnexpectedNonWhitespaceText509Test$Data[""])
[ERROR]   UnexpectedNonWhitespaceText509Test.testDeSerMetaData:88->deSer:106 Unexpected non-whitespace text ('Text Editor' in Array context: should not occur (or should be handled)
 at [Source: (StringReader); line: 2, column: 44] (through reference chain: com.fasterxml.jackson.dataformat.xml.deser.UnexpectedNonWhitespaceText509Test$MetaData["data"])
ptziegler commented 11 months ago

Here's also the updated test run + stack trace.

com.fasterxml.jackson.dataformat.xml.deser.UnexpectedNonWhitespaceText509Test.txt

cowtowncoder commented 11 months ago

Ok: one quick note -- this is bit problematic as it has JAXB/Jakarta-RS annotations, which are only supported when appropriate module is registered, which is not done here. But it also ideally should be avoided as XML module does not have direct dependency to JAXB/Jakarta-RS annotations module; we do have test dep to JAXB one but not the other yet.

It'd be good to instead use Jackson annotations as there are equivalents for all annotations that are supported.

I hope to have time to look into this.

cowtowncoder commented 8 months ago

Ok seems to use Java 9 methods too, will need to modify a bit.

cowtowncoder commented 8 months ago

Ok this is not supportable at all: Jackson does not support concept of "any" XML content that content property here is trying to do.

With 2.16.0-SNAPSHOT I do get exception for mismatched property, which relates to textual content within <data>: this can be mapped using @XmlText (or whatever it was in JAXB), or, @JacksonXmlText. But it has to be String or similar type; there is no mechanism for collecting multiple segments. Nor is there any plans to try to do that. It is part of JAXB/Jakarta-binding that Jackson does not and will not support (support is only for subset of annotations, not for model itself).

What can be done is that whole <data> can be mapped to either Map<String, Object> (of java.lang.Object which becomes Map) or JsonNode. These are only construct that support true mixed content.

I will close this PR and will see whether #509 itself is worth investigating.

cowtowncoder commented 8 months ago

Hmmh. Actually I take this back. After using @JacksonXmlText I can see the exception that seems wrong. Maybe this is usable after all.

cowtowncoder commented 8 months ago

Ok, I think the problem is not the assertion with exception, but state handling; text segment should be handled same as within regular Object; it is not directly within Array context. So I think that with changes this reproduction is useful; may merge as failing test and hopefully have time to figure it out.