Description
Jackson: 2.18.2
I consider an interface with @JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, property = "type")
.
It works correctly with implementations in thr same package, but it fails with implementations in a child package.
Can be reproduced with:
package eu.solven.adhoc.jackson;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, property = "type")
public interface IParent {
}
OK:
package eu.solven.adhoc.jackson;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Children implements IParent {
String someField;
public String getSomeField() {
return someField;
}
public void setSomeField(String someField) {
this.someField = someField;
}
public static void main(String[] args) {
ObjectMapper om = new ObjectMapper();
Map<?, ?> asMap = om.convertValue(new Children(), Map.class);
System.out.println(asMap);
IParent asParent = om.convertValue(asMap, IParent.class);
}
}
KO:
package eu.solven.adhoc.jackson.sub;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.solven.adhoc.jackson.IParent;
public class ChildrenSubPackage implements IParent {
String someField;
public String getSomeField() {
return someField;
}
public void setSomeField(String someField) {
this.someField = someField;
}
public static void main(String[] args) {
ObjectMapper om = new ObjectMapper();
Map<?, ?> asMap = om.convertValue(new ChildrenSubPackage(), Map.class);
System.out.println(asMap);
IParent asParent = om.convertValue(asMap, IParent.class);
}
}
Could not resolve type id 'eu.solven.adhoc.jackson.ChildrenSubPackage' as a subtype of
eu.solven.adhoc.jackson.IParent
: no such class found at [Source: UNKNOWN; byte offset: #UNKNOWN]
Indeed, the type is invalid in {type=.ChildrenSubPackage, someField=null}
as it should be .sub.ChildrenSubPackage
.
Also, the javadoc seems invalid:
For example, for supertype "com.foobar.Base", and concrete type "com.foo.Impl", only ".Impl" would be included; and for "com.foo.impl.Impl2" only ".impl.Impl2" would be included.
I suppose we want to read:
For example, for supertype "com.foo.Base", and concrete type "com.foo.Impl", only ".Impl" would be included; and for "com.foo.impl.Impl2" only ".impl.Impl2" would be included.