-
-
Notifications
You must be signed in to change notification settings - Fork 221
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
Strange behavior when using XML attributes #206
Comments
Ok, first things first: not checking of the outermost element is an implementation quirk; element name is not verified. This could be considered a flaw, or a convenience feature; if it was verified, there would be need to figure out expect name from class. As to attributes: the problem is that addition of an attribute will essentially force handling of the XML element to expect a POJO, not scalar value. Or put another way, scalar values may only be mapped from simple XML elements. I don't remember if there are attempts to make this particular case work, however (I recall something along those lines), but fundamentally this is problematic usage: where would value of I think there are flaws in handling, so that cases (3) and (4) should either fail to indicate mismatch (if attribute and value case can not be supported), or work. So there are things to investigate. Above just tries to explain what is happening, and not to claim it's the way it should work. |
OK @Getter
@Setter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class In {
@JacksonXmlProperty(localName = "first")
private XmlNode first;
@JacksonXmlProperty(localName = "second")
private XmlNode second;
} And @Getter
@Setter
@ToString
@JsonIgnoreProperties(ignoreUnknown = true)
public class XmlNode {
@JacksonXmlText
private String value;
//still ignoring any other attributes
} solving my problems. I'm not trying it now, because previously I have very similar issue with parsing XML (#191). And that time |
Or maybe it's better to throw some exception in case of such situation? because now if we add attribute to some field we get weird behavior. |
@latsyk there should indeed be an exception to indicate actual problem, and not just quietly swallow or fail. I'll reopen this issue to see if there's a way to better catch the problem. |
I am actually not quite sure what to do here. Trying output with attributes, POJOs, suggests that code will figure it out (by not trying to write things as attributes when it can not). |
Hi @cowtowncoder , My jackson version 2.9.3:
Kotlin version : I too am facing an issue while deserialising an element with an attribute. Example XML that needs to be parsed :
classes that I am using :
Now in the first element that has both attribute value and textual content. whilst parsing I get this exception: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of which is really weird. Coz I have other elements with just attribute value and all of them work just fine. Only this element which has both attribute value and textual content doesn't work. |
My jackson version 2.8.1:
I want to parse XML like this:
So my Out class:
In class:
And App class:
For XML above everything is good, I get
Out(ins=[In(first=fff, second=sss), In(first=fff2, second=sss2)])
.But I found few issues:
If I change
Out
element name to something else everything is working, is it OK?I get
Out(ins=[In(first=fff, second=sss), In(first=fff2, second=sss2)])
Lets add some attribute to first element:
And this fail with
Can not construct instance of xmlparsing.In: no String-argument constructor/factory method to deserialize from String value ('sss')
.More stranger that if we swap elements then everything will be good:
I get
Out(ins=[In(first=fff, second=sss)])
But this swap can't help me for a long, because:
I get only second one :
Out(ins=[In(first=fff2, second=sss2)])
One more strange thing:
I get two elements:
Out(ins=[In(first=fff, second=null), In(first=null, second=null)])
. Withoutlang="en"
everything is good.The text was updated successfully, but these errors were encountered: