Skip to content

Commit acd2e38

Browse files
committed
Fix #1919
1 parent 620d889 commit acd2e38

File tree

4 files changed

+30
-15
lines changed

4 files changed

+30
-15
lines changed

release-notes/VERSION-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Project: jackson-databind
88

99
#792: Deserialization Not Working Right with Generic Types and Builders
1010
(reported by Mike G; fix contributed by Ville K)
11+
#1919: Abstract class included as part of known type ids for error message
12+
when using JsonSubTypes
13+
(reported by Incara@github)
1114
#2091: `ReferenceType` does not expose valid containedType
1215
(reported by Nate B)
1316
#2683: Explicitly fail (de)serialization of `java.time.*` types in absence of

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeDeserializerBase.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public abstract class TypeDeserializerBase
2222
implements java.io.Serializable
2323
{
2424
private static final long serialVersionUID = 1;
25-
25+
2626
protected final TypeIdResolver _idResolver;
27-
27+
2828
protected final JavaType _baseType;
2929

3030
/**
@@ -47,9 +47,9 @@ public abstract class TypeDeserializerBase
4747
* in cases where type id is to be exposed as part of JSON.
4848
*/
4949
protected final String _typePropertyName;
50-
50+
5151
protected final boolean _typeIdVisible;
52-
52+
5353
/**
5454
* For efficient operation we will lazily build mappings from type ids
5555
* to actual deserializers, once needed.

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeNameIdResolver.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,15 @@ protected JavaType _typeFromId(String id) {
164164

165165
@Override
166166
public String getDescForKnownTypeIds() {
167-
return new TreeSet<String>(_idToType.keySet()).toString();
167+
// 05-May-2020, tatu: As per [databind#1919], only include ids for
168+
// non-abstract types
169+
final TreeSet<String> ids = new TreeSet<>();
170+
for (Map.Entry<String, JavaType> entry : _idToType.entrySet()) {
171+
if (entry.getValue().isConcrete()) {
172+
ids.add(entry.getKey());
173+
}
174+
}
175+
return ids.toString();
168176
}
169177

170178
@Override

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

+14-10
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
import java.util.*;
66

7-
import com.fasterxml.jackson.annotation.JsonProperty;
8-
import com.fasterxml.jackson.annotation.JsonSubTypes;
9-
import com.fasterxml.jackson.annotation.JsonTypeInfo;
10-
import com.fasterxml.jackson.annotation.JsonTypeName;
7+
import com.fasterxml.jackson.annotation.*;
118
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
12-
import com.fasterxml.jackson.databind.JsonMappingException;
9+
1310
import com.fasterxml.jackson.databind.ObjectMapper;
1411
import com.fasterxml.jackson.databind.SerializationFeature;
1512
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
@@ -95,7 +92,9 @@ public POJOWrapper(Sub sub1, Sub sub2) {
9592

9693
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=As.PROPERTY, property="type")
9794
@JsonSubTypes({ @JsonSubTypes.Type(ImplX.class),
98-
@JsonSubTypes.Type(ImplY.class) })
95+
@JsonSubTypes.Type(ImplY.class),
96+
@JsonSubTypes.Type(ImplAbs.class)
97+
})
9998
static abstract class BaseX { }
10099

101100
@JsonTypeName("x")
@@ -111,6 +110,11 @@ static class ImplY extends BaseX {
111110
public int y;
112111
}
113112

113+
// for [databind#919] testing
114+
@JsonTypeName("abs")
115+
abstract static class ImplAbs extends BaseX {
116+
}
117+
114118
// [databind#663]
115119
static class AtomicWrapper {
116120
public BaseX value;
@@ -372,16 +376,16 @@ public void testDefaultImplViaModule() throws Exception
372376
bean = mapper.readValue("{\"#type\":\"foobar\"}", SuperTypeWithoutDefault.class);
373377
assertEquals(DefaultImpl505.class, bean.getClass());
374378
assertEquals(0, ((DefaultImpl505) bean).a);
375-
376379
}
377-
380+
378381
public void testErrorMessage() throws Exception {
379382
ObjectMapper mapper = new ObjectMapper();
380383
try {
381384
mapper.readValue("{ \"type\": \"z\"}", BaseX.class);
382385
fail("Should have failed");
383-
} catch (JsonMappingException e) {
384-
verifyException(e, "known type ids =");
386+
} catch (InvalidTypeIdException e) {
387+
verifyException(e, "Could not resolve type id 'z' as a subtype of");
388+
verifyException(e, "known type ids = [x, y]");
385389
}
386390
}
387391

0 commit comments

Comments
 (0)