blob: 2fa0523a45cf0949029e793caf5481f54a8efd45 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package com.fasterxml.jackson.databind.jsontype;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.*;
public class PolymorphicDeserErrorHandlingTest extends BaseMapTest
{
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY,
property = "clazz")
abstract static class BaseForUnknownClass {
}
static class BaseUnknownWrapper {
public BaseForUnknownClass value;
}
// [databind#2668]
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Child1.class, name = "child1"),
@JsonSubTypes.Type(value = Child2.class, name = "child2")
})
static class Parent2668 {
}
static class Child1 extends Parent2668 {
public String bar;
}
static class Child2 extends Parent2668 {
public String baz;
}
/*
/**********************************************************************
/* Test methods
/**********************************************************************
*/
private final ObjectMapper MAPPER = newJsonMapper();
public void testUnknownClassAsSubtype() throws Exception
{
ObjectReader reader = MAPPER.readerFor(BaseUnknownWrapper.class)
.without(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
BaseUnknownWrapper w = reader.readValue(aposToQuotes
("{'value':{'clazz':'com.foobar.Nothing'}}'"));
assertNotNull(w);
}
// [databind#2668]
public void testSubType2668() throws Exception
{
String json = "{\"type\": \"child2\", \"baz\":\"1\"}"; // JSON for Child2
try {
/*Child1 c =*/ MAPPER.readValue(json, Child1.class); // Deserializing into Child1
fail("Should not pass");
} catch (JsonMappingException e) {
verifyException(e, "not subtype of");
}
}
}
|