diff options
author | Tatu Saloranta <tatu.saloranta@iki.fi> | 2020-02-27 19:52:22 -0800 |
---|---|---|
committer | Tatu Saloranta <tatu.saloranta@iki.fi> | 2020-02-27 19:52:22 -0800 |
commit | 771f6c6b939b78923a3df95c39b5593f8ba2ad09 (patch) | |
tree | 0ead58a52202029431bf6461ebfdfd17c179d341 | |
parent | cb922358a72b13867291a972c14bc8c5530140cf (diff) | |
download | jackson-databind-771f6c6b939b78923a3df95c39b5593f8ba2ad09.tar.gz |
First improvement wrt #2632: change to give more meaningful fail message.
-rw-r--r-- | src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java | 9 | ||||
-rw-r--r-- | src/test/java/com/fasterxml/jackson/databind/jsontype/SubTypeResolutionTest.java (renamed from src/test/java/com/fasterxml/jackson/databind/jsontype/SubTypeResolution1964Test.java) | 45 |
2 files changed, 48 insertions, 6 deletions
diff --git a/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java b/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java index c6a3e7788..7252fc973 100644 --- a/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java +++ b/src/main/java/com/fasterxml/jackson/databind/type/TypeFactory.java @@ -474,9 +474,12 @@ public class TypeFactory // note: was final in 2.9, removed from 2.10 { List<JavaType> expectedTypes = sourceType.getBindings().getTypeParameters(); List<JavaType> actualTypes = actualType.getBindings().getTypeParameters(); - for (int i = 0, len = expectedTypes.size(); i < len; ++i) { + + final int actCount = actualTypes.size(); + + for (int i = 0, expCount = expectedTypes.size(); i < expCount; ++i) { JavaType exp = expectedTypes.get(i); - JavaType act = actualTypes.get(i); + JavaType act = (i < actCount) ? actualTypes.get(i) : unknownType(); if (!_verifyAndResolvePlaceholders(exp, act)) { // 14-May-2018, tatu: As per [databind#2034] it seems we better relax assignment @@ -505,7 +508,7 @@ public class TypeFactory // note: was final in 2.9, removed from 2.10 } } return String.format("Type parameter #%d/%d differs; can not specialize %s with %s", - (i+1), len, exp.toCanonical(), act.toCanonical()); + (i+1), expCount, exp.toCanonical(), act.toCanonical()); } } return null; diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/SubTypeResolution1964Test.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/SubTypeResolutionTest.java index fa9ef01e7..1b29a77bb 100644 --- a/src/test/java/com/fasterxml/jackson/databind/jsontype/SubTypeResolution1964Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/SubTypeResolutionTest.java @@ -3,10 +3,13 @@ package com.fasterxml.jackson.databind.jsontype; import java.util.*; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; + import com.fasterxml.jackson.databind.*; /** - * Test for [databind#1964], wherein slightly incompatible type hierarchy, + * Originally test for [databind#1964], wherein slightly incompatible type hierarchy, * where `Map` key is downcast from `String` to `Object` (via use of "raw" * types to force compiler to ignore incompatibility) causes exception * during serialization. Although ideally code would not force round peg @@ -14,7 +17,7 @@ import com.fasterxml.jackson.databind.*; * such downcast just for Map key types (for now at least). */ @SuppressWarnings("serial") -public class SubTypeResolution1964Test extends BaseMapTest +public class SubTypeResolutionTest extends BaseMapTest { // [databind#1964] static class AccessModel { @@ -65,7 +68,26 @@ public class SubTypeResolution1964Test extends BaseMapTest static class MetaAttribute<M, V, B> extends AbstractMetaValue<M, V, B> { public MetaAttribute() { } } - + + // [databind#2632]: fail to specialize type-erased + @SuppressWarnings("rawtypes") + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME) + @JsonSubTypes(value = { + @JsonSubTypes.Type(value = Either.Left.class, name = "left"), + @JsonSubTypes.Type(value = Either.Right.class, name = "right") + }) + static class Either<L, R> { + static class Left<T> extends Either { } + static class Right<T> extends Either { } + } + + static class Foo { + @SuppressWarnings("unchecked") + public Either<String, String> getEither() { + return new Either.Right<String>(); + } + } + /* /********************************************************************** /* Unit tests @@ -103,4 +125,21 @@ public class SubTypeResolution1964Test extends BaseMapTest // ... could/should verify more, perhaps, but for now let it be. assertNotNull(jsonStr); } + + // [databind#2632]: fail to specialize type-erased + public void testSpecializeIncompatibleRawType() throws Exception + { + // 27-Feb-2020, tatu: First things first; incompatible typing should + // cause reasonable exception + // ... although since it's writing, perhaps should NOT fail at all? + String json; + + try { + json = MAPPER.writeValueAsString(new Foo()); + assertNotNull(json); + fail("Should not (yet?) pass"); + } catch (JsonMappingException e) { + verifyException(e, "Failed to specialize base type "); + } + } } |