Skip to content

Commit f8ed660

Browse files
committed
Merge branch '2.7'
2 parents f77b53c + ebadfd2 commit f8ed660

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
@@ -441,6 +441,8 @@ Yoann Rodière (fenrhil@github)
441441
Mark Woon (markwoon@github)
442442
* Reported #1178: `@JsonSerialize(contentAs=superType)` behavior disallowed in 2.7
443443
(2.7.4)
444+
* Reported #1231: `@JsonSerialize(as=superType)` behavior disallowed in 2.7.4
445+
(2.7.5)
444446

445447
Tom Mack (tommack@github)
446448
* 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
@@ -46,6 +46,8 @@ Project: jackson-databind
4646
(reported by Nick B)
4747
#1228: @JsonAnySetter does not deserialize null to Deserializer's NullValue
4848
(contributed by Eric S)
49+
#1231: `@JsonSerialize(as=superType)` behavior disallowed in 2.7.4
50+
(reported by Mark W)
4951

5052
2.7.4 (29-Apr-2016)
5153

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,10 +847,19 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
847847
// static typing this way
848848
type = type.withStaticTyping();
849849
} else {
850+
Class<?> currRaw = type.getRawClass();
850851
try {
851852
// 11-Oct-2015, tatu: For deser, we call `TypeFactory.constructSpecializedType()`,
852853
// 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+
}
854863
} catch (IllegalArgumentException iae) {
855864
throw new JsonMappingException(null,
856865
String.format("Failed to widen type %s with annotation (value %s), from '%s': %s",
@@ -869,8 +878,20 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
869878
if (keyType.hasRawClass(keyClass)) {
870879
keyType = keyType.withStaticTyping();
871880
} else {
881+
Class<?> currRaw = keyType.getRawClass();
872882
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+
}
874895
} catch (IllegalArgumentException iae) {
875896
throw new JsonMappingException(null,
876897
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)