Skip to content

Commit f2c40cb

Browse files
committed
Add a test for handling of "unknown type id"
1 parent 33ba4ad commit f2c40cb

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,12 @@ public JavaType handleUnknownTypeId(JavaType baseType, String id,
889889
if (type.hasRawClass(Void.class)) {
890890
return null;
891891
}
892-
return type;
892+
// But ensure there's type compatibility
893+
if (type.isTypeOrSubTypeOf(baseType.getRawClass())) {
894+
return type;
895+
}
896+
throw unknownTypeException(baseType, id,
897+
"problem handler tried to resolve into non-subtype: "+type);
893898
}
894899
h = h.next();
895900
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fasterxml.jackson.databind.filter;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
6+
import com.fasterxml.jackson.databind.*;
7+
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
8+
9+
public class ProblemHandlerTest extends BaseMapTest
10+
{
11+
static class TypeIdHandler
12+
extends DeserializationProblemHandler
13+
{
14+
@Override
15+
public JavaType handleUnknownTypeId(DeserializationContext ctxt,
16+
JavaType baseType, String subTypeId,
17+
String failureMsg)
18+
throws IOException
19+
{
20+
return ctxt.constructType(Impl.class);
21+
}
22+
}
23+
24+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
25+
static class Base { }
26+
static class Impl extends Base {
27+
public int a;
28+
}
29+
30+
static class BaseWrapper {
31+
public Base value;
32+
}
33+
34+
/*
35+
/**********************************************************
36+
/* Test methods
37+
/**********************************************************
38+
*/
39+
40+
public void testInvalidTypeId() throws Exception
41+
{
42+
ObjectMapper mapper = new ObjectMapper()
43+
.addHandler(new TypeIdHandler());
44+
BaseWrapper w = mapper.readValue("{\"value\":{\"type\":\"foo\",\"a\":4}}",
45+
BaseWrapper.class);
46+
assertNotNull(w);
47+
}
48+
}

src/test/java/com/fasterxml/jackson/databind/filter/TestUnknownPropertyDeserialization.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public TestBean() { }
4242
* just marks unknown property/ies when encountered, along with
4343
* Json value of the property.
4444
*/
45-
final static class MyHandler
45+
static class MyHandler
4646
extends DeserializationProblemHandler
4747
{
4848
@Override

0 commit comments

Comments
 (0)