1
1
package com .fasterxml .jackson .dataformat .xml .deser ;
2
2
3
+ import com .fasterxml .jackson .databind .ObjectReader ;
3
4
import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
4
5
import com .fasterxml .jackson .dataformat .xml .XmlTestBase ;
5
6
import com .fasterxml .jackson .dataformat .xml .annotation .JacksonXmlProperty ;
@@ -26,6 +27,11 @@ static class Nested2 {
26
27
String value ;
27
28
}
28
29
30
+ // [dataformat-xml#579]
31
+ static class Bean579 {
32
+ public String str ;
33
+ }
34
+
29
35
/*
30
36
/**********************************************************************
31
37
/* Test methods
@@ -97,4 +103,42 @@ public void testValidStructure() throws Exception {
97
103
assertEquals ("test" , value .nested .nested2 .attr );
98
104
assertEquals ("Some text" , value .nested .nested2 .value );
99
105
}
106
+
107
+ // [dataformat-xml#579]
108
+ public void testEmptyRootElem579 () throws Exception
109
+ {
110
+ Bean579 bean ;
111
+
112
+ ObjectReader R = MAPPER .readerFor (Bean579 .class );
113
+
114
+ // By default, no coercion of empty element
115
+ bean = R .readValue ("<Content/>" );
116
+ assertNotNull (bean );
117
+ assertNull (bean .str );
118
+
119
+ // So same as non-empty
120
+ bean = R .readValue ("<Content></Content>" );
121
+ assertNotNull (bean );
122
+ assertNull (bean .str );
123
+
124
+ // But enabling feature we can coerce POJO into null:
125
+
126
+ // 29-May-2023, tatu: Alas! Note that we CANNOT use ObjectReader because
127
+ // FormatFeature (FromXmlParser.Feature) overrides ARE NOT APPLIED EARLY
128
+ // ENOUGH to take effect. Instead we must configure XmlMapper
129
+
130
+
131
+ //R = R.with(FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL);
132
+
133
+ R = mapperBuilder ().enable (FromXmlParser .Feature .EMPTY_ELEMENT_AS_NULL )
134
+ .build ()
135
+ .readerFor (Bean579 .class );
136
+ bean = R .readValue ("<Content/>" );
137
+ assertNull (bean );
138
+
139
+ // which won't affect non-empty variant
140
+ bean = R .readValue ("<Content></Content>" );
141
+ assertNotNull (bean );
142
+ assertNull (bean .str );
143
+ }
100
144
}
0 commit comments