-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Problem deserializing polymorphic types with @JsonCreator #113
Comments
This is not much information to go about. Do you have actual exception message, indicating which Java type triggers this? Presumably it is a class that uses |
I don't have actual exception message. But from debugging I gather it's the root class of my hierarchy which is abstract. Let me know if there is something else you would like to know. |
Could you share the root object? To fix this, I will need to be able to reproduce it, and at this point I can't. |
Here you go: https://gist.github.com/4113543 |
Thanks! |
Ok. Use of creator methods may be problematic with polymorphic types, so although this should work, a temporary work-around would be to change 'id' to be passed via field or (private) setter. |
I added a check to at least indicate the root cause ("could not find creator property with name 'id'") here, as the first step. |
Great. Do you plan to correct this problem for the next release? |
If possible yes, i.e. my best intention is to do that. But it all depends on what exactly is going on. I would recommend however to work around the issue in the meantime by moving id not to be passed via creator. I suspect that combination (abstract types, creator methods) is triggering the issue. |
Another workaround for this issue is to ensure that you use an interface as the base rather than an abstract class. Then decorate the interface with @JsonTypeInfo. |
I bumped in to this when playing around with code along the lines of: public class DummyTest {
@Test
public void test() throws JsonParseException, JsonMappingException, IOException {
new ObjectMapper()
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.readValue("{\"foo\":\"blah\",\"bar\":\"bleh\"}", Sample.class);
}
public static class Sample {
@JsonProperty
private String foo;
@JsonProperty
private String bar;
public Sample(@JsonProperty("foo") String foo, @JsonProperty("bar") String bar) {
this.bar = bar;
this.foo = foo;
}
}
} |
@chids What problem are you seeing here? This should just work fine, as there is no polymorphic type to consider? |
@cowtowncoder I stumbled on a NPE in From my, somewhat narrow, point of view the culprit seems to be: But I also noticed that the error went away if I removed any one of the constructor arguments in the code above (thus leaving the constructor with only one argument). |
@chids: handling of a single-non-annotated-ctor-argument is different: this will be considered so-called "delegating" creator, meaning that Jackson will actually bind full matching value and pass that; whereas with named parameters, JSON content must be a JSON Object, and properties are extracted from that. So your work-around does change semantics of handling. You are right in that if auto-discovery is disabled, it has effect on handling of constructors that could otherwise be located as per rules (single-String, single-int/long/Integer/Long constructors). So: to fix your example, you must add |
Oh. Actually, one more thing: delegating-constructor should also NOT be auto-detected, with your configuration. So there may be a small flaw in there. |
@cowtowncoder ok, thanks! |
With 2.3.0-SNAPSHOT error now becomes: com.fasterxml.jackson.databind.JsonMappingException: Could not find creator property with name 'id' (in class com.fasterxml.jackson.failing.TestIssueGH113$Animal) which suggests that combination of polymorphic types and creator methods has its issues still. |
Bump. (Also, I just found a workaround, but there still seems to be some bug here.) |
Thank you for adding the link -- hope this helps until we can find a better solution. |
Ha. Turns out fix was easy; just needed to block handling of creator properties for non-concrete types. Exception came from handler of abstract base type, where handling itself is completely unnecessary. |
I get a NPE when trying to de-serialize a hierarchy of objects. Here is the stack trace:
This exception does not happen with versions of jackson-databind prior to 2.1.0. I've tested with versions: 2.0.2, 2.0.4, 2.0.5, 2.0.6.
Let me know if you need more information.
The text was updated successfully, but these errors were encountered: