@@ -847,10 +847,19 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
847
847
// static typing this way
848
848
type = type .withStaticTyping ();
849
849
} else {
850
+ Class <?> currRaw = type .getRawClass ();
850
851
try {
851
852
// 11-Oct-2015, tatu: For deser, we call `TypeFactory.constructSpecializedType()`,
852
853
// may be needed here too in future?
853
- type = tf .constructGeneralizedType (type , serClass );
854
+ if (serClass .isAssignableFrom (currRaw )) { // common case
855
+ type = tf .constructGeneralizedType (type , serClass );
856
+ } else if (currRaw .isAssignableFrom (serClass )) { // specialization, ok as well
857
+ type = tf .constructSpecializedType (type , serClass );
858
+ } else {
859
+ throw new JsonMappingException (null ,
860
+ String .format ("Can not refine serialization type %s into %s; types not related" ,
861
+ type , serClass .getName ()));
862
+ }
854
863
} catch (IllegalArgumentException iae ) {
855
864
throw new JsonMappingException (null ,
856
865
String .format ("Failed to widen type %s with annotation (value %s), from '%s': %s" ,
@@ -869,8 +878,20 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
869
878
if (keyType .hasRawClass (keyClass )) {
870
879
keyType = keyType .withStaticTyping ();
871
880
} else {
881
+ Class <?> currRaw = keyType .getRawClass ();
872
882
try {
873
- keyType = tf .constructGeneralizedType (keyType , keyClass );
883
+ // 19-May-2016, tatu: As per [databind#1231], [databind#1178] may need to actually
884
+ // specialize (narrow) type sometimes, even if more commonly opposite
885
+ // is needed.
886
+ if (keyClass .isAssignableFrom (currRaw )) { // common case
887
+ keyType = tf .constructGeneralizedType (keyType , keyClass );
888
+ } else if (currRaw .isAssignableFrom (keyClass )) { // specialization, ok as well
889
+ keyType = tf .constructSpecializedType (keyType , keyClass );
890
+ } else {
891
+ throw new JsonMappingException (null ,
892
+ String .format ("Can not refine serialization key type %s into %s; types not related" ,
893
+ keyType , keyClass .getName ()));
894
+ }
874
895
} catch (IllegalArgumentException iae ) {
875
896
throw new JsonMappingException (null ,
876
897
String .format ("Failed to widen key type of %s with concrete-type annotation (value %s), from '%s': %s" ,
0 commit comments