Skip to content

Commit cc53828

Browse files
committed
Fixed #1098
1 parent 1068506 commit cc53828

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

release-notes/VERSION

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

77
2.7.5 (not yet released)
88

9+
#1098: DeserializationFeature.FAIL_ON_INVALID_SUBTYPE does not work with
10+
`JsonTypeInfo.Id.CLASS`
11+
(reported by szaccaria@github)
912
#1223: `BasicClassIntrospector.forSerialization(...).findProperties` should
1013
respect MapperFeature.AUTO_DETECT_GETTERS/SETTERS?
1114
(reported by William H)

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

+20-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
import com.fasterxml.jackson.annotation.JsonTypeInfo;
66
import com.fasterxml.jackson.databind.DatabindContext;
7+
import com.fasterxml.jackson.databind.DeserializationConfig;
8+
import com.fasterxml.jackson.databind.DeserializationFeature;
79
import com.fasterxml.jackson.databind.JavaType;
10+
import com.fasterxml.jackson.databind.cfg.MapperConfig;
811
import com.fasterxml.jackson.databind.type.TypeFactory;
912
import com.fasterxml.jackson.databind.util.ClassUtil;
1013

@@ -40,33 +43,44 @@ public String idFromValueAndType(Object value, Class<?> type) {
4043
@Deprecated // since 2.3
4144
@Override
4245
public JavaType typeFromId(String id) {
43-
return _typeFromId(id, _typeFactory);
46+
return _typeFromId(id, null);
4447
}
4548

4649
@Override
4750
public JavaType typeFromId(DatabindContext context, String id) {
48-
return _typeFromId(id, context.getTypeFactory());
51+
return _typeFromId(id, context);
4952
}
5053

51-
protected JavaType _typeFromId(String id, TypeFactory typeFactory)
54+
protected JavaType _typeFromId(String id, DatabindContext ctxt)
5255
{
5356
/* 30-Jan-2010, tatu: Most ids are basic class names; so let's first
5457
* check if any generics info is added; and only then ask factory
5558
* to do translation when necessary
5659
*/
60+
TypeFactory tf = (ctxt == null) ? _typeFactory : ctxt.getTypeFactory();
5761
if (id.indexOf('<') > 0) {
58-
JavaType t = typeFactory.constructFromCanonical(id);
62+
JavaType t = tf.constructFromCanonical(id);
5963
// note: may want to try combining with specialization (esp for EnumMap)?
6064
return t;
6165
}
66+
Class<?> cls;
6267
try {
63-
Class<?> cls = typeFactory.findClass(id);
64-
return typeFactory.constructSpecializedType(_baseType, cls);
68+
cls = tf.findClass(id);
6569
} catch (ClassNotFoundException e) {
70+
// 24-May-2016, tatu: Ok, this is supremely ugly, from multiple persepctives.
71+
// But fixes [databind#1098], so has to do for now.
72+
MapperConfig<?> cfg = ctxt.getConfig();
73+
if (cfg instanceof DeserializationConfig) {
74+
DeserializationConfig dc = (DeserializationConfig) cfg;
75+
if (!dc.isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)) {
76+
return null;
77+
}
78+
}
6679
throw new IllegalArgumentException("Invalid type id '"+id+"' (for id type 'Id.class'): no such class found");
6780
} catch (Exception e) {
6881
throw new IllegalArgumentException("Invalid type id '"+id+"' (for id type 'Id.class'): "+e.getMessage(), e);
6982
}
83+
return tf.constructSpecializedType(_baseType, cls);
7084
}
7185

7286
/*

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.databind.jsontype.impl;
22

33
import com.fasterxml.jackson.annotation.JsonTypeInfo;
4+
import com.fasterxml.jackson.databind.DatabindContext;
45
import com.fasterxml.jackson.databind.JavaType;
56
import com.fasterxml.jackson.databind.type.TypeFactory;
67

@@ -48,7 +49,7 @@ public String idFromValue(Object value)
4849
}
4950

5051
@Override
51-
protected JavaType _typeFromId(String id, TypeFactory typeFactory)
52+
protected JavaType _typeFromId(String id, DatabindContext ctxt)
5253
{
5354
if (id.startsWith(".")) {
5455
StringBuilder sb = new StringBuilder(id.length() + _basePackageName.length());
@@ -61,6 +62,6 @@ protected JavaType _typeFromId(String id, TypeFactory typeFactory)
6162
}
6263
id = sb.toString();
6364
}
64-
return super._typeFromId(id, typeFactory);
65+
return super._typeFromId(id, ctxt);
6566
}
6667
}

0 commit comments

Comments
 (0)