@@ -22,23 +22,24 @@ static class NestedImpl implements Nested {
22
22
public String value ;
23
23
}
24
24
25
+ private final ObjectMapper MAPPER = newObjectMapper ();
26
+
25
27
@ Test
26
28
public void testPolymorphicType () throws Exception
27
29
{
28
30
// first, with value
29
31
String YAML = "nested:\n "
30
32
+" type: single\n "
31
33
+" value: whatever" ;
32
- ObjectMapper mapper = new ObjectMapper (new YAMLFactory ());
33
- Wrapper top = mapper .readValue (YAML , Wrapper .class );
34
+ Wrapper top = MAPPER .readValue (YAML , Wrapper .class );
34
35
assertNotNull (top );
35
36
assertEquals (NestedImpl .class , top .nested .getClass ());
36
37
assertEquals ("whatever" , ((NestedImpl ) top .nested ).value );
37
38
38
39
// then without value
39
40
YAML = "nested:\n "
40
41
+" type: single" ;
41
- top = mapper .readValue (YAML , Wrapper .class );
42
+ top = MAPPER .readValue (YAML , Wrapper .class );
42
43
assertNotNull (top );
43
44
assertEquals (NestedImpl .class , top .nested .getClass ());
44
45
assertNull ("whatever" , ((NestedImpl ) top .nested ).value );
@@ -49,26 +50,27 @@ public void testNativePolymorphicType() throws Exception {
49
50
String YAML = "nested: !single\n "
50
51
+" value: foobar\n "
51
52
;
52
- ObjectMapper mapper = new ObjectMapper (new YAMLFactory ());
53
- Wrapper top = mapper .readValue (YAML , Wrapper .class );
53
+ Wrapper top = MAPPER .readValue (YAML , Wrapper .class );
54
54
assertNotNull (top );
55
55
assertNotNull (top .nested );
56
56
assertEquals (NestedImpl .class , top .nested .getClass ());
57
57
assertEquals ("foobar" , ((NestedImpl ) top .nested ).value );
58
58
59
- YAML = "nested: !single { }\n " ;
60
- top = mapper .readValue (YAML , Wrapper .class );
59
+ top = MAPPER .readValue ("nested: !single { }\n " , Wrapper .class );
61
60
assertNotNull (top );
62
61
assertNotNull (top .nested );
63
62
assertEquals (NestedImpl .class , top .nested .getClass ());
63
+ }
64
+
65
+ @ Test
66
+ public void testNativePolymorphicTypeFromEmpty () throws Exception {
64
67
// no value specified, empty
65
68
66
69
// And third possibility; trickier, since YAML contains empty String,
67
70
// and not Object; so we need to allow coercion
68
- ObjectReader r = mapper .readerFor (Wrapper .class )
71
+ ObjectReader r = MAPPER .readerFor (Wrapper .class )
69
72
.with (DeserializationFeature .ACCEPT_EMPTY_STRING_AS_NULL_OBJECT );
70
- YAML = "nested: !single\n " ;
71
- top = r .readValue (YAML );
73
+ Wrapper top = r .readValue ("nested: !single\n " );
72
74
assertNotNull (top );
73
75
74
76
// and as a result, get null
0 commit comments