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

try to fix part of #4668 #4669

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.Modifier;
import java.util.*;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
Expand Down Expand Up @@ -174,15 +175,15 @@ public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> c
NamedType rootType = new NamedType(rawBase, null);
AnnotatedClass ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
rawBase);
_collectAndResolveByTypeId(ac, rootType, config, typesHandled, byName);
_collectAndResolveByTypeId(null, ac, rootType, config, typesHandled, byName);

// then with definitions from property
if (property != null) {
Collection<NamedType> st = ai.findSubtypes(property);
if (st != null) {
for (NamedType nt : st) {
ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config, nt.getType());
_collectAndResolveByTypeId(ac, nt, config, typesHandled, byName);
_collectAndResolveByTypeId(null, ac, nt, config, typesHandled, byName);
}
}
}
Expand All @@ -193,7 +194,7 @@ public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> c
if (rawBase.isAssignableFrom(subtype.getType())) { // yes
AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
subtype.getType());
_collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName);
_collectAndResolveByTypeId(null, curr, subtype, config, typesHandled, byName);
}
}
}
Expand All @@ -209,15 +210,15 @@ public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> c
Map<String,NamedType> byName = new LinkedHashMap<String,NamedType>();

NamedType rootType = new NamedType(rawBase, null);
_collectAndResolveByTypeId(baseType, rootType, config, typesHandled, byName);
_collectAndResolveByTypeId(null, baseType, rootType, config, typesHandled, byName);

if (_registeredSubtypes != null) {
for (NamedType subtype : _registeredSubtypes) {
// is it a subtype of root type?
if (rawBase.isAssignableFrom(subtype.getType())) { // yes
AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
subtype.getType());
_collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName);
_collectAndResolveByTypeId(null, curr, subtype, config, typesHandled, byName);
}
}
}
Expand Down Expand Up @@ -276,11 +277,20 @@ protected void _collectAndResolve(AnnotatedClass annotatedType, NamedType namedT
* Method called to find subtypes for a specific type (class), using
* type id as the unique key (in case of conflicts).
*/
protected void _collectAndResolveByTypeId(AnnotatedClass annotatedType, NamedType namedType,
protected void _collectAndResolveByTypeId(JsonTypeInfo.Value rootTypeInfo, AnnotatedClass annotatedType, NamedType namedType,
MapperConfig<?> config,
Set<Class<?>> typesHandled, Map<String,NamedType> byName)
{
final AnnotationIntrospector ai = config.getAnnotationIntrospector();
if (rootTypeInfo != null) {
JsonTypeInfo.Value currentTi = ai.findPolymorphicTypeInfo(config, annotatedType);
if (currentTi != null && !currentTi.equals(rootTypeInfo)) {
// we shouldn't resolve it if a subtypes is defined with @JsonTypeInfo and
// its definition is not compatible with the superclass.
return;
}
}

if (!namedType.hasName()) {
String name = ai.findTypeName(annotatedType);
if (name != null) {
Expand All @@ -295,10 +305,11 @@ protected void _collectAndResolveByTypeId(AnnotatedClass annotatedType, NamedTyp
if (typesHandled.add(namedType.getType())) {
Collection<NamedType> st = ai.findSubtypes(annotatedType);
if (st != null && !st.isEmpty()) {
rootTypeInfo = rootTypeInfo == null ? ai.findPolymorphicTypeInfo(config, annotatedType) : rootTypeInfo;
for (NamedType subtype : st) {
AnnotatedClass subtypeClass = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
subtype.getType());
_collectAndResolveByTypeId(subtypeClass, subtype, config, typesHandled, byName);
_collectAndResolveByTypeId(rootTypeInfo, subtypeClass, subtype, config, typesHandled, byName);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,32 @@ public void testRoundTripMap() throws Exception
}
}
}

@Test
public void testInheritingDifferentJsonType() {
ObjectMapper mapper = new ObjectMapper();
Collection<NamedType> subtypes = new StdSubtypeResolver().collectAndResolveSubtypesByTypeId(
mapper.getDeserializationConfig(),
// note: `null` is fine here as `AnnotatedMember`:
null,
mapper.constructType(Animal.class));
boolean containsHumanity2 = false;
boolean containsHumanity3 = false;
for (NamedType type : subtypes) {
Class<?> clz = type.getType();
if (clz == Humanity.class) {
fail("Type '"+Humanity.class+"' shouldn't be resolved");
}
if (clz == Humanity2.class) {
containsHumanity2 = true;
}
if (clz == Humanity3.class) {
containsHumanity3 = true;
}
}
assertTrue(containsHumanity2, "Type '"+Humanity2.class+"' must be resolved");
assertTrue(containsHumanity3, "Type '"+Humanity3.class+"' must be resolved");
}
}

/*
Expand All @@ -128,7 +154,10 @@ public void testRoundTripMap() throws Exception
@JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
@JsonSubTypes({
@Type(value=Dog.class, name="doggy"),
@Type(Cat.class) /* defaults to "TestTypedNames$Cat" then */
@Type(Cat.class), /* defaults to "TestTypedNames$Cat" then */
@Type(value = Mammal.class, name = "mammal"),
@Type(value = Mammal2.class, name = "mammal2"),
@Type(value = Mammal3.class, name = "mammal3")
})
class Animal
{
Expand Down Expand Up @@ -207,3 +236,37 @@ public Persian(String n, boolean p) {
}
}

@JsonTypeInfo(use = Id.NAME, property = "mammalType")
@JsonSubTypes({
@Type(value = Humanity.class, name = "humanity")
})
class Mammal extends Animal {
public String mammalType;
}

class Humanity extends Mammal {

}

@JsonSubTypes({
@Type(value = Humanity2.class, name = "humanity2")
})
class Mammal2 extends Animal {

}

class Humanity2 extends Mammal {

}

@JsonTypeInfo(use=Id.NAME, include=As.WRAPPER_OBJECT)
@JsonSubTypes({
@Type(value = Humanity3.class, name = "humanity3")
})
class Mammal3 extends Animal {

}

class Humanity3 extends Mammal {

}