Skip to content

Commit ebadfd2

Browse files
committed
Fix #1231
1 parent 9d3de8c commit ebadfd2

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

release-notes/CREDITS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ Yoann Rodière (fenrhil@github)
439439
Mark Woon (markwoon@github)
440440
* Reported #1178: `@JsonSerialize(contentAs=superType)` behavior disallowed in 2.7
441441
(2.7.4)
442+
* Reported #1231: `@JsonSerialize(as=superType)` behavior disallowed in 2.7.4
443+
(2.7.5)
442444

443445
Tom Mack (tommack@github)
444446
* Reported #1208: treeToValue doesn't handle POJONodes that contain exactly

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Project: jackson-databind
1010
(reported by Nick B)
1111
#1228: @JsonAnySetter does not deserialize null to Deserializer's NullValue
1212
(contributed by Eric S)
13+
#1231: `@JsonSerialize(as=superType)` behavior disallowed in 2.7.4
14+
(reported by Mark W)
1315

1416
2.7.4 (29-Apr-2016)
1517

src/main/java/com/fasterxml/jackson/databind/AnnotationIntrospector.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,19 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
813813
// static typing this way
814814
type = type.withStaticTyping();
815815
} else {
816+
Class<?> currRaw = type.getRawClass();
816817
try {
817818
// 11-Oct-2015, tatu: For deser, we call `TypeFactory.constructSpecializedType()`,
818819
// 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+
}
820829
} catch (IllegalArgumentException iae) {
821830
throw new JsonMappingException(null,
822831
String.format("Failed to widen type %s with annotation (value %s), from '%s': %s",
@@ -835,8 +844,20 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
835844
if (keyType.hasRawClass(keyClass)) {
836845
keyType = keyType.withStaticTyping();
837846
} else {
847+
Class<?> currRaw = keyType.getRawClass();
838848
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+
}
840861
} catch (IllegalArgumentException iae) {
841862
throw new JsonMappingException(null,
842863
String.format("Failed to widen key type of %s with concrete-type annotation (value %s), from '%s': %s",

src/test/java/com/fasterxml/jackson/databind/ser/TestJsonSerialize.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void testBrokenAnnotation() throws Exception
149149
try {
150150
serializeAsString(MAPPER, new BrokenClass());
151151
} catch (Exception e) {
152-
verifyException(e, "not a super-type of");
152+
verifyException(e, "types not related");
153153
}
154154
}
155155

0 commit comments

Comments
 (0)