Skip to content

Commit 7752ea7

Browse files
committed
Fix #2632
1 parent 09ce4be commit 7752ea7

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,10 @@ Oleksandr Poslavskyi (alevskyi@github)
10891089
property name if `MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES` is enabled
10901090
(2.11.0)
10911091
1092+
Simone D'Avico (simonedavico@github)
1093+
* Reported #2632: Failure to resolve generic type parameters on serialization
1094+
(2.11.0)
1095+
10921096
Robin Roos (robinroos@github)
10931097
* Contributed #2636: ObjectReader readValue lacks Class<T> argument
10941098
(2.11.0)

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Project: jackson-databind
4242
(reported by Bartosz B)
4343
#2592: `ObjectMapper.setSerializationInclusion()` is ignored for `JsonAnyGetter`
4444
(reported by Oleksii K)
45+
#2632: Failure to resolve generic type parameters on serialization
46+
(reported by Simone D)
4547
#2636: ObjectReader readValue lacks Class<T> argument
4648
(contributed by Robin R)
4749
#2643: Change default textual serialization of `java.util.Date`/`Calendar`

src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ public JavaType constructSpecializedType(JavaType baseType, Class<?> subclass,
449449
break;
450450
}
451451
// (4) If all else fails, do the full traversal using placeholders
452-
TypeBindings tb = _bindingsForSubtype(baseType, typeParamCount, subclass);
452+
TypeBindings tb = _bindingsForSubtype(baseType, typeParamCount,
453+
subclass, relaxedCompatibilityCheck);
453454
newType = _fromClass(null, subclass, tb);
454455

455456
} while (false);
@@ -460,7 +461,8 @@ public JavaType constructSpecializedType(JavaType baseType, Class<?> subclass,
460461
return newType;
461462
}
462463

463-
private TypeBindings _bindingsForSubtype(JavaType baseType, int typeParamCount, Class<?> subclass)
464+
private TypeBindings _bindingsForSubtype(JavaType baseType, int typeParamCount,
465+
Class<?> subclass, boolean relaxedCompatibilityCheck)
464466
{
465467
PlaceholderForType[] placeholders = new PlaceholderForType[typeParamCount];
466468
for (int i = 0; i < typeParamCount; ++i) {
@@ -479,8 +481,12 @@ private TypeBindings _bindingsForSubtype(JavaType baseType, int typeParamCount,
479481
// and traverse type hierarchies to both verify and to resolve placeholders
480482
String error = _resolveTypePlaceholders(baseType, baseWithPlaceholders);
481483
if (error != null) {
482-
throw new IllegalArgumentException("Failed to specialize base type "+baseType.toCanonical()+" as "
483-
+subclass.getName()+", problem: "+error);
484+
// 28-Mar-2020, tatu: As per [databind#2632], need to ignore the issue in
485+
// some cases. For now, just fully ignore; may need to refine in future
486+
if (!relaxedCompatibilityCheck) {
487+
throw new IllegalArgumentException("Failed to specialize base type "+baseType.toCanonical()+" as "
488+
+subclass.getName()+", problem: "+error);
489+
}
484490
}
485491

486492
final JavaType[] typeParams = new JavaType[typeParamCount];

src/test/java/com/fasterxml/jackson/databind/jsontype/SubTypeResolutionTest.java

+2-12
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,7 @@ public void testTypeSpecialization2034() throws Exception
129129
// [databind#2632]: fail to specialize type-erased
130130
public void testSpecializeIncompatibleRawType() throws Exception
131131
{
132-
// 27-Feb-2020, tatu: First things first; incompatible typing should
133-
// cause reasonable exception
134-
// ... although since it's writing, perhaps should NOT fail at all?
135-
String json;
136-
137-
try {
138-
json = MAPPER.writeValueAsString(new Foo());
139-
assertNotNull(json);
140-
fail("Should not (yet?) pass");
141-
} catch (JsonMappingException e) {
142-
verifyException(e, "Failed to specialize base type ");
143-
}
132+
String json = MAPPER.writeValueAsString(new Foo());
133+
assertNotNull(json);
144134
}
145135
}

0 commit comments

Comments
 (0)