-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
Jackson fails to instantiate class when only text node is present #608
Comments
Have a look at these search results - https://github.com/search?q=repo%3AFasterXML%2Fjackson-dataformat-xml%20JsonIgnore&type=code The JsonIgnore annotation and variants of it are what are used in Jackson to ignore fields. |
I don't want to ignore the field. This is a very real field that is sometimes there and sometimes not there. And I need access to the value inside it if it is there. |
This looks like the correct behaviour to me. You have a mechanism to get this to work (the In the end of the day, you have to accept that libs work a certain way. Jackson is very pluggable - write your own custom deserializer if you don't like the built-in behaviour. Or there are dozens of alternative libs for deserializing XML. |
No this is definitely not correct behavior. And as far as I can tell there is no way to configure the class to get the data to parse. Just to be clearer on what can happen here, these are both valid XML snippets:
Both snippets should be parsable using two classes where nested contains one |
Can you find the docs that describe that the code is supposed to work the way you want? If not, then this is not a bug - it is a change request. |
I'm not interested in a debate over semantics or what rules have been defined somewhere. If you wish to call it a change request instead of a bug thats fine by me. At the moment I see no way to write a set of classes that can be used to parse the above two snippets. And both snippets are valid XML and structurally similar. I'm fine with an alternative configuration using whatever settings in Jackson, field naming, classes, or annotations may exists. I just want access to my data. |
Write a custom deserializer if you can't get it to work with the default jackson-dataformat-xml code. There are examples online of how to write Jackson custom deserializers. Or try another XML lib. Jackson is a JSON lib that has some support for other formats. There is no great community around jackson-dataformat-xml. It is what it is. Even if someone feels the need to do something on this issue, it could be weeks, months or years until that happens. |
Quick note on POJO: you are declaring private (and, But other than that, handling Mixed Content (both XML Text and XML Element(s) at same level) is very difficult for data-binding. It is possible some cases may not be supportable. Still, what would help here would be fail reproduction using only Jackson annotations (use of JAXB annotations for actual use is fine, but for unit tests we ideally need Jackson equivalents). This would eliminate possibility of issue being due to JAXB annotation support (and if so, moving issue there) and avoid problem with cross module dependencies (XML module does not depend on JAXB annotation support). |
The classes are marked with the @XmlAccessorType(FIELD) annotation which instructs the serializer to set the fields directly instead of going through a constructor or setter. I prefer this way since there is less code and it makes it clear to the reader that these classes are immutable DTOs and only set through serialization.
The configuration is unambiguous and works well in all other circumstances. If the child tag exists Jackson can parse the XML just fine. The @XmlValue annotation is being used the way it should and Jackson clearly supports it. And if the child tag isn't there the field could be set to null, the same way a field is normally set to null when a tag is absent.
I want it to work with JAXB annotations (for reasons..). That said, I automatically assumed it would be the parser itself where the problem lies instead of the annotation project. Should I create another bug report at |
No need to create/move issue, but the test case really needs to use Jackson annotations. This should be fine as there are equivalents. But on fields: Jackson does not recognize or use I don't think either of these things is the root cause of the issue. |
@cowtowncoder You are right JAXB was an unnecessary complication. The same problem exists when using just jackson annotations and the standard XmlMapper. To make you happy I also removed the finals :). I edited the original question at the top with the new code. |
Thank you @codemonstur |
The following code generates an exception:
Setting fields to private or public has no effect. If I add the tag the parsing works fine. If I remove the other and reallyNotHere fields the parsing works fine. But when the child tags are NOT there and the field IS there I get this Exception:
Jackson version: 2.15.2
The text was updated successfully, but these errors were encountered: