-
-
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
Required attribute of @JsonProperty
is ignored when deserializing from XML
#538
Comments
Hmmh. That should work, unless I am missing something. Perhaps this is because of the oddities of the root element handling, in context of "empty element"; it could be that processing is unintentionally short-cut to go through default constructor or something. I hope to look into this in near future (but there's a bit of a backlog): thank you for reporting the issue, and especially thank you for providing the test case! |
The parsing of an empty XML element(1 tag, no start and end tag) is correct, if the feature FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL is enabled.
The parsing of XML element with value empty string (start and end tag with empty string between), with feature FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL enabled, fails. The result is an object (Bar: 0) created by the default constructor, the required property foo is zero (= default value). There is no MismatchedInputException thrown for the missing foo property.
The parsing of XML element with value empty string (start and end tag with empty string between) is correct, if the coercion configuration is defined for type Bar in order to process an empty string input as null.
|
@johandeschutterGET Yes, you are right. Apologies for slow follow up here, I am finally catching up here. Ok. Looking at this, what happens is that this is essentially coercion from Empty String into "empty" value of POJO. To further complicate things, however, from databind perspective (format-agnostic), value at this point definitely looks like String value, not Object. But maybe I can figure out something... |
Also: I think this is specific to Root value as well: it would not be (as) problematic for anything referenced by some other value. Unfortunately I am not sure of how to tackle this at this point in time -- while it would be possible to detect existence of "property-based" Creator (via Unfortunately I think behavior was actually changed in 2.12 from such a setup to exposing as JSON String to support root-level scalars... So going back to that might be tricky to pull off. Unless we could somehow figure out target for root value is POJO, based on deserializer? |
Thinking about this more: translation is within The problem is that at this point we do not (yet, sort of) have information on So likely it'd have to be done, instead, from It seems somewhat doable, just a bit complicated and convoluted :) |
As per above note, I think #491 is due to the same root problem. So solving this issue would resolve at least 2 reported issues. |
@JsonProperty
is ignored when deserializing from XML
Ok, I was able to figure out a much simpler approach, essentially reverting some of the changes done in 2.12 for coercion root-element textual content. But without breaking what that change was initially added to fix. So should work as expected in 2.14.0; hoping to release 2.14.0-rc1 within a week or so. |
When JSON is parsed, there is a check on properties that are annotated with JsonProperty with attibute required to true.
If the required property is missing, a MismatchedInputException with message "Missing required creator property" is thrown.
When XML is parsed, there is no check on required properties/element. If the required property/element is missing, there is no exception.
Adding DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES doesn't make any difference.
There is constructor is annotated with JsonCreator and with the required property annotated with JsonProperty.
It worked when jackson-dataformat-xml version 2.11.4 was used, it doesn't work since version 2.12.0
Do I need to use another annotation for a required XML element?
Example class:
Example of unit test on JSON, this unit test throws an exception as expected, because property foo is missing.
Example of unit test on XML, this unit test does not throw an exception
The text was updated successfully, but these errors were encountered: