Skip to content
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

@JsonUnwrapped causes deserializer to silently ignore unknown properties #1061

Closed
kdkeck opened this issue Dec 30, 2015 · 4 comments
Closed

Comments

@kdkeck
Copy link

kdkeck commented Dec 30, 2015

Adding @JsonUnwrapped to any field in a class prevents an UnrecognizedPropertyException from being thrown, ever, regardless of DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES or @JsonIgnoreProperties. (As of databind version 2.5.4.)

If you look at UnwrappedPropertyHandler.processUnwrapped(JsonParser, DeserializationContext, Object, TokenBuffer), you'll see that it iterates over the unwrapped properties and sets each one based on properties found in the TokenBuffer. Nowhere does it check that every property stashed in the TokenBuffer was found in (at least) one of the unwrapped properties.

@kdkeck
Copy link
Author

kdkeck commented Dec 30, 2015

Another bug in unwrapped property handling (in both BeanDeserializer.deserializeWithUnwrapped() methods) is that all the properties stashed in the TokenBuffer are also sent to the any setter, regardless of whether they'll later be consumed in processUnwrapped().

Note for anyone else encountering this: as a hacky partial work-around for the first bug, this means you may be able to add a @JsonAnySetter method to your class (or modify the existing one, if you already have one) to throw an UnrecognizedPropertyException if the property name isn't in a list of the unwrapped subproperty names. But of course then that list will have to be maintained in sync with your model. It also won't work if the properties on the unwrapped property value can't all be deserialized with the same binding type (e.g., Object or JsonNode, neither of which seems to work for array- or collection-valued properties). And it causes a potentially non-trivial amount of extra work to be performed, to deserialize those property values for the any setter as well as in processUnwrapped().

@cowtowncoder
Copy link
Member

I think this may dup of #650?

Yes, unwrapped handling has no way of determining if something is or is not unknown, because deserializers are not required to indicate if such set is limited or not: for example, use of @JsonAnySetter allows indefinite set of properties to be accepted. Similarly, Maps can contain any and all Object properties. There have been additions to BeanDeserializer that may allow some level of additional introspection that perhaps could be used to improve handling.

Whether this behavior may or may not be changed is not clear at this point; from internal perspective this may be a hard limitation.

@kdkeck
Copy link
Author

kdkeck commented Dec 30, 2015

Yes, I looked for an existing issue and didn't find that one, but it is a dup.

@kdkeck kdkeck closed this as completed Dec 30, 2015
@cowtowncoder
Copy link
Member

Quite understandable, there are a few issues and original is quite old.
Feel free to add notes on original ones if details are missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants