Skip to content

Commit 700617a

Browse files
committed
Fix #1125
1 parent a290f09 commit 700617a

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Project: jackson-databind
66

77
2.7.3 (not yet released)
88

9+
#1125: Problem with polymorphic types, losing properties from base type(s)
910
#1150: Problem with Object id handling, explicit `null` token
1011
(reported by Xavi T)
1112

src/main/java/com/fasterxml/jackson/databind/type/SimpleType.java

+33-6
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,43 @@ protected JavaType _narrow(Class<?> subclass)
127127
}
128128
// Should we check that there is a sub-class relationship?
129129
// 15-Jan-2016, tatu: Almost yes, but there are some complications with
130-
// placeholder values, so no.
131-
/*
130+
// placeholder values (`Void`, `NoClass`), so can not quite do yet.
131+
// TODO: fix in 2.8
132132
if (!_class.isAssignableFrom(subclass)) {
133+
/*
133134
throw new IllegalArgumentException("Class "+subclass.getName()+" not sub-type of "
134135
+_class.getName());
136+
*/
137+
return new SimpleType(subclass, _bindings, this, _superInterfaces,
138+
_valueHandler, _typeHandler, _asStatic);
135139
}
136-
*/
137-
// 15-Jan-2015, tatu: Not correct; should really re-resolve...
138-
return new SimpleType(subclass, _bindings, this, _superInterfaces,
139-
_valueHandler, _typeHandler, _asStatic);
140+
// Otherwise, stitch together the hierarchy. First, super-class
141+
Class<?> next = subclass.getSuperclass();
142+
if (next == _class) { // straight up parent class? Great.
143+
return new SimpleType(subclass, _bindings, this,
144+
_superInterfaces, _valueHandler, _typeHandler, _asStatic);
145+
}
146+
if ((next != null) && _class.isAssignableFrom(next)) {
147+
JavaType superb = _narrow(next);
148+
return new SimpleType(subclass, _bindings, superb,
149+
null, _valueHandler, _typeHandler, _asStatic);
150+
}
151+
// if not found, try a super-interface
152+
Class<?>[] nextI = subclass.getInterfaces();
153+
for (Class<?> iface : nextI) {
154+
if (iface == _class) { // directly implemented
155+
return new SimpleType(subclass, _bindings, null,
156+
new JavaType[] { this }, _valueHandler, _typeHandler, _asStatic);
157+
}
158+
if (_class.isAssignableFrom(iface)) { // indirect, so recurse
159+
JavaType superb = _narrow(iface);
160+
return new SimpleType(subclass, _bindings, null,
161+
new JavaType[] { superb }, _valueHandler, _typeHandler, _asStatic);
162+
}
163+
}
164+
// should not get here but...
165+
throw new IllegalArgumentException("Internal error: Can not resolve sub-type for Class "+subclass.getName()+" to "
166+
+_class.getName());
140167
}
141168

142169
@Override

src/test/java/com/fasterxml/jackson/databind/jsontype/TestPolymorphicWithDefaultImpl.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void testDeserializationWithArrayOfSize2() throws Exception
160160
assertEquals(Arrays.asList("a", "b"), ((MyInter) inter).blah);
161161
}
162162

163-
// [Databind#148]
163+
// [databind#148]
164164
public void testDefaultAsNoClass() throws Exception
165165
{
166166
Object ob = MAPPER.readerFor(DefaultWithNoClass.class).readValue("{ }");
@@ -178,7 +178,7 @@ public void testDefaultAsVoid() throws Exception
178178
assertNull(ob);
179179
}
180180

181-
// [Databind#148]
181+
// [databind#148]
182182
public void testBadTypeAsNull() throws Exception
183183
{
184184
ObjectMapper mapper = new ObjectMapper();
@@ -189,7 +189,7 @@ public void testBadTypeAsNull() throws Exception
189189
assertNull(ob);
190190
}
191191

192-
// [Databind#511]
192+
// [databind#511]
193193
public void testInvalidTypeId511() throws Exception {
194194
ObjectReader reader = MAPPER.reader().without(
195195
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE,
@@ -203,7 +203,7 @@ public void testInvalidTypeId511() throws Exception {
203203
assertNotNull(badResult);
204204
}
205205

206-
// [Databind#656]
206+
// [databind#656]
207207
public void testDefaultImplWithObjectWrapper() throws Exception
208208
{
209209
BaseFor656 value = MAPPER.readValue(aposToQuotes("{'foobar':{'a':3}}"), BaseFor656.class);

0 commit comments

Comments
 (0)