Skip to content

Commit

Permalink
Merge pull request #1650 from slobo-showbie/2.8
Browse files Browse the repository at this point in the history
Fix #1647: Missing properties from base class when recursive types are involved
  • Loading branch information
cowtowncoder authored Jun 9, 2017
2 parents 93f7e14 + b214b98 commit 3d74347
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public void setReference(JavaType ref)
}
_referencedType = ref;
}

@Override
public JavaType getSuperClass() {
if (_referencedType != null) {
return _referencedType.getSuperClass();
}
return super.getSuperClass();
}

public JavaType getSelfReferencedType() { return _referencedType; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ static class HashTree<K, V> extends HashMap<K, HashTree<K, V>> { }
// for [databind#938]
public static interface Ability<T> { }

// for [databind#1647]
static interface IFace<T> {}

// for [databind#1647]
static class Base implements IFace<Sub> { }

// for [databind#1647]
static class Sub extends Base { }

public static final class ImmutablePair<L, R> implements Map.Entry<L, R>, Ability<ImmutablePair<L, R>> {
public final L key;
public final R value;
Expand Down Expand Up @@ -93,4 +102,14 @@ public void testJavaTypeToString() throws Exception
fail("Description should contain 'recursive type', did not: "+desc);
}
}

// for [databind#1647]
public void testSuperClassWithReferencedJavaType() {
TypeFactory tf = objectMapper().getTypeFactory();
tf.constructType(Base.class); // must be constructed before sub to set the cache correctly
JavaType subType = tf.constructType(Sub.class);
// baseTypeFromSub should be a ResolvedRecursiveType in this test
JavaType baseTypeFromSub = subType.getSuperClass();
assertNotNull(baseTypeFromSub.getSuperClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.fasterxml.jackson.databind.type;

import java.io.IOException;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.type.TypeFactory;


// https://github.com/FasterXML/jackson-databind/issues/1647
public class TestTypeFactoryWithRecursiveTypes extends BaseMapTest {

static interface IFace<T> {
}

static class Base implements IFace<Sub> {
@JsonProperty int base = 1;
}

static class Sub extends Base {
@JsonProperty int sub = 2;
}

public void testBasePropertiesIncludedWhenSerializingSubWhenSubTypeLoadedAfterBaseType() throws IOException {
TypeFactory tf = TypeFactory.defaultInstance();
tf.constructType(Base.class);
tf.constructType(Sub.class);
Sub sub = new Sub();
String serialized = objectMapper().writeValueAsString(sub);
assertEquals("{\"base\":1,\"sub\":2}", serialized);
}
}

0 comments on commit 3d74347

Please sign in to comment.