Skip to content

Commit 0dde658

Browse files
committed
Add a test wrt FasterXML#579
1 parent 679c793 commit 0dde658

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/test/java/com/fasterxml/jackson/dataformat/xml/deser/EmptyBeanDeser318Test.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.dataformat.xml.deser;
22

3+
import com.fasterxml.jackson.databind.ObjectReader;
34
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
45
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
56
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
@@ -26,6 +27,11 @@ static class Nested2 {
2627
String value;
2728
}
2829

30+
// [dataformat-xml#579]
31+
static class Bean579 {
32+
public String str;
33+
}
34+
2935
/*
3036
/**********************************************************************
3137
/* Test methods
@@ -97,4 +103,42 @@ public void testValidStructure() throws Exception {
97103
assertEquals("test", value.nested.nested2.attr);
98104
assertEquals("Some text", value.nested.nested2.value);
99105
}
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+
}
100144
}

0 commit comments

Comments
 (0)