@@ -813,10 +813,19 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
813
813
// static typing this way
814
814
type = type .withStaticTyping ();
815
815
} else {
816
+ Class <?> currRaw = type .getRawClass ();
816
817
try {
817
818
// 11-Oct-2015, tatu: For deser, we call `TypeFactory.constructSpecializedType()`,
818
819
// may be needed here too in future?
819
- type = tf .constructGeneralizedType (type , serClass );
820
+ if (serClass .isAssignableFrom (currRaw )) { // common case
821
+ type = tf .constructGeneralizedType (type , serClass );
822
+ } else if (currRaw .isAssignableFrom (serClass )) { // specialization, ok as well
823
+ type = tf .constructSpecializedType (type , serClass );
824
+ } else {
825
+ throw new JsonMappingException (null ,
826
+ String .format ("Can not refine serialization type %s into %s; types not related" ,
827
+ type , serClass .getName ()));
828
+ }
820
829
} catch (IllegalArgumentException iae ) {
821
830
throw new JsonMappingException (null ,
822
831
String .format ("Failed to widen type %s with annotation (value %s), from '%s': %s" ,
@@ -835,8 +844,20 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
835
844
if (keyType .hasRawClass (keyClass )) {
836
845
keyType = keyType .withStaticTyping ();
837
846
} else {
847
+ Class <?> currRaw = keyType .getRawClass ();
838
848
try {
839
- keyType = tf .constructGeneralizedType (keyType , keyClass );
849
+ // 19-May-2016, tatu: As per [databind#1231], [databind#1178] may need to actually
850
+ // specialize (narrow) type sometimes, even if more commonly opposite
851
+ // is needed.
852
+ if (keyClass .isAssignableFrom (currRaw )) { // common case
853
+ keyType = tf .constructGeneralizedType (keyType , keyClass );
854
+ } else if (currRaw .isAssignableFrom (keyClass )) { // specialization, ok as well
855
+ keyType = tf .constructSpecializedType (keyType , keyClass );
856
+ } else {
857
+ throw new JsonMappingException (null ,
858
+ String .format ("Can not refine serialization key type %s into %s; types not related" ,
859
+ keyType , keyClass .getName ()));
860
+ }
840
861
} catch (IllegalArgumentException iae ) {
841
862
throw new JsonMappingException (null ,
842
863
String .format ("Failed to widen key type of %s with concrete-type annotation (value %s), from '%s': %s" ,
0 commit comments