jackson-databind: External property polymorphic deserialization does not work with enums
versions: Jackson 2.8.1, Jackson-module-kotlin 2.8.1
Attempting to deserialize a class using external_property. In my case, the property is an Enum type with values matching the type name. Now that issue #999 is fixed, I thought this would work, but now I’m getting a different error:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of enum.Invite, problem: argument type mismatch
at [Source: {
"kind": "CONTACT",
"to": {
"name": "Foo"
}
}; line: 6, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:268)
at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:1405)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.wrapAsJsonMappingException(StdValueInstantiator.java:468)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.rewrapCtorProblem(StdValueInstantiator.java:487)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:276)
at com.fasterxml.jackson.module.kotlin.KotlinValueInstantiator.createFromObjectWith(KotlinValueInstantiator.kt:30)
at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:135)
at com.fasterxml.jackson.databind.deser.impl.ExternalTypeHandler.complete(ExternalTypeHandler.java:225)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeUsingPropertyBasedWithExternalTypeId(BeanDeserializer.java:937)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeWithExternalTypeId(BeanDeserializer.java:792)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:312)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2852)
at enum.Reproduction_KindEnumKt.main(Reproduction-KindEnum.kt:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.fasterxml.jackson.databind.introspect.AnnotatedConstructor.call(AnnotatedConstructor.java:124)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromObjectWith(StdValueInstantiator.java:274)
... 15 more
Process finished with exit code 1
Here is the reproduction recipe: https://github.com/rocketraman/jackson-issue-enum-polymorphism/blob/master/src/main/kotlin/enumtype/Reproduction-KindEnum.kt
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 23 (19 by maintainers)
Commits related to this issue
- Add test for #1328 (not failing, alas) — committed to FasterXML/jackson-databind by cowtowncoder 7 years ago
- Fix #1328 — committed to FasterXML/jackson-databind by cowtowncoder 6 years ago
Ok so this took a while. But I found the problem and fixed it for 2.9.6. As @apatrida pointed out earlier, value was of wrong type –
String
– and needs to be routed via deserializer (first added into temporary buffer). Doing that resolves this case at least; I did not see other code paths so I hope this is sufficient.@pmorixe Unfortunately I still don’t see the issue here: no exception is thrown. What is the exact problem you see?
Note that
type
is metadata-only, by default, so its value should not be visible to properties, unlessvisible=true
is set for@JsonTypeInfo
. But even if that is done, I do not see failure. So I think I’d like to see some assertion(s) in test. I tested this with 2.8.8 / 2.9.0.pr3, but I don’t assume this is different from 2.8.4.@LukeButtersFunnelback How about I re-open it, given that there is a Java reproduction.
Looking at this again now I realize that it is not clear whether type id is even expected to allow anything other than
String
s. I never intended it to be used for anything other than simple strings, for what that is worth – if it has worked, that is actually surprising…But I’ll try to see if it’d be easy to make this work. It is not an unreasonable wish.