Skip to content

Commit 8bda9ba

Browse files
committed
Fixed #374
1 parent 38a1312 commit 8bda9ba

File tree

8 files changed

+49
-25
lines changed

8 files changed

+49
-25
lines changed

release-notes/CREDITS-2.x

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ Jochen Schalanda (joschi@github)
115115
* Reported #318: XMLMapper fails to deserialize null (POJO reference) from blank tag
116116
(2.12.0)
117117

118+
Ingo Wiarda (dewarim@github)
119+
120+
* Reported #374: Deserialization fails with `XmlMapper` and
121+
`DeserializationFeature.UNWRAP_ROOT_VALUE`
122+
(2.12.0)
123+
118124
Ghenadii Batalski (ghenadiibatalski@github)
119125

120126
* Reported #377: `ToXmlGenerator` ignores `Base64Variant` while serializing `byte[]`

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Project: jackson-dataformat-xml
3535
(reported by Jochen S)
3636
#319: Empty root tag into `List` deserialization bug
3737
(reported by Seatec13@github)
38+
#374: Deserialization fails with `XmlMapper` and `DeserializationFeature.UNWRAP_ROOT_VALUE`
39+
(reported by Ingo W)
3840
#377: `ToXmlGenerator` ignores `Base64Variant` while serializing `byte[]`
3941
(reported by Ghenadii B)
4042
#390: Unexpected attribute at string fields causes extra objects to be

src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlDeserializationContext.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ public DefaultDeserializationContext with(DeserializerFactory factory) {
7777
/**********************************************************
7878
*/
7979

80+
// [dataformat-xml#374]: need to remove handling of expected root name unwrapping
81+
// to match serialization
82+
@Override // since 2.12
83+
public Object readRootValue(JsonParser p, JavaType valueType,
84+
JsonDeserializer<Object> deser, Object valueToUpdate)
85+
throws IOException
86+
{
87+
// if (_config.useRootWrapping()) {
88+
// return _unwrapAndDeserialize(p, valueType, deser, valueToUpdate);
89+
// }
90+
if (valueToUpdate == null) {
91+
return deser.deserialize(p, this);
92+
}
93+
return deser.deserialize(p, this, valueToUpdate);
94+
}
95+
96+
// To support case where XML element has attributes as well as CDATA, need
97+
// to "extract" scalar value (CDATA), after the fact
8098
@Override // since 2.12
8199
public String extractScalarFromObject(JsonParser p, JsonDeserializer<?> deser,
82100
Class<?> scalarType)

src/main/java/com/fasterxml/jackson/dataformat/xml/ser/XmlSerializerProvider.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ public class XmlSerializerProvider extends DefaultSerializerProvider
2929
// As of 2.7
3030
private static final long serialVersionUID = 1L;
3131

32-
/**
33-
* If all we get to serialize is a null, there's no way to figure out
34-
* expected root name; so let's just default to literal {@code "null"}.
35-
*/
36-
protected final static QName ROOT_NAME_FOR_NULL = new QName("null");
37-
3832
protected final XmlRootNameLookup _rootNameLookup;
3933

4034
public XmlSerializerProvider(XmlRootNameLookup rootNames)
@@ -218,20 +212,20 @@ public void serializePolymorphic(JsonGenerator gen, Object value, JavaType rootT
218212
}
219213
}
220214

221-
protected void _serializeXmlNull(JsonGenerator jgen) throws IOException
215+
protected void _serializeXmlNull(JsonGenerator gen) throws IOException
222216
{
223217
// 14-Nov-2016, tatu: As per [dataformat-xml#213], we may have explicitly
224218
// configured root name...
225219
QName rootName = _rootNameFromConfig();
226220
if (rootName == null) {
227-
rootName = ROOT_NAME_FOR_NULL;
221+
rootName = XmlRootNameLookup.ROOT_NAME_FOR_NULL;
228222
}
229-
if (jgen instanceof ToXmlGenerator) {
230-
_initWithRootName((ToXmlGenerator) jgen, rootName);
223+
if (gen instanceof ToXmlGenerator) {
224+
_initWithRootName((ToXmlGenerator) gen, rootName);
231225
}
232-
super.serializeValue(jgen, null);
226+
super.serializeValue(gen, null);
233227
}
234-
228+
235229
protected void _startRootArray(ToXmlGenerator xgen, QName rootName) throws IOException
236230
{
237231
xgen.writeStartObject();
@@ -241,10 +235,8 @@ protected void _startRootArray(ToXmlGenerator xgen, QName rootName) throws IOExc
241235

242236
protected void _initWithRootName(ToXmlGenerator xgen, QName rootName) throws IOException
243237
{
244-
/* 28-Nov-2012, tatu: We should only initialize the root
245-
* name if no name has been set, as per [dataformat-xml#42],
246-
* to allow for custom serializers to work.
247-
*/
238+
// 28-Nov-2012, tatu: We should only initialize the root name if no name has been
239+
// set, as per [dataformat-xml#42], to allow for custom serializers to work.
248240
if (!xgen.setNextNameIfMissing(rootName)) {
249241
// however, if we are root, we... insist
250242
if (xgen.inRoot()) {

src/main/java/com/fasterxml/jackson/dataformat/xml/util/XmlRootNameLookup.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public class XmlRootNameLookup
1818
{
1919
private static final long serialVersionUID = 1L;
2020

21+
/**
22+
* If all we get to serialize is a null, there's no way to figure out
23+
* expected root name; so let's just default to literal {@code "null"}.
24+
*/
25+
public final static QName ROOT_NAME_FOR_NULL = new QName("null");
26+
2127
/**
2228
* For efficient operation, let's try to minimize number of times we
2329
* need to introspect root element name to use.

src/test/java/com/fasterxml/jackson/dataformat/xml/failing/EnumIssue9Test.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.fasterxml.jackson.annotation.JsonTypeInfo;
44

5-
import com.fasterxml.jackson.databind.ObjectMapper;
6-
75
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
86
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
97

@@ -29,14 +27,13 @@ public UntypedEnumBean() { }
2927
/**********************************************************
3028
*/
3129

32-
private final XmlMapper MAPPER = new XmlMapper();
33-
30+
private final XmlMapper MAPPER = newMapper();
31+
3432
public void testUntypedEnum() throws Exception
3533
{
36-
ObjectMapper mapper = new XmlMapper();
37-
String xml = mapper.writeValueAsString(new UntypedEnumBean(TestEnum.B));
34+
String xml = MAPPER.writeValueAsString(new UntypedEnumBean(TestEnum.B));
3835

39-
UntypedEnumBean result = mapper.readValue(xml, UntypedEnumBean.class);
36+
UntypedEnumBean result = MAPPER.readValue(xml, UntypedEnumBean.class);
4037
assertNotNull(result);
4138
assertNotNull(result.value);
4239
Object ob = result.value;

src/test/java/com/fasterxml/jackson/dataformat/xml/failing/Issue37AdapterTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import com.fasterxml.jackson.dataformat.xml.*;
1414
import com.fasterxml.jackson.dataformat.xml.jaxb.XmlJaxbAnnotationIntrospector;
1515

16+
// 30-Jun-2020, tatu: This is deferred and possibly won't be fixed
17+
// at all. But leaving failing test here just in case future brings
18+
// alternate approach to do something.
1619
public class Issue37AdapterTest extends XmlTestBase
1720
{
1821
@XmlJavaTypeAdapter(URLEncoderMapDataAdapter.class)

src/test/java/com/fasterxml/jackson/dataformat/xml/failing/Unwrapped374Test.java renamed to src/test/java/com/fasterxml/jackson/dataformat/xml/misc/RootNameWrapping374Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
package com.fasterxml.jackson.dataformat.xml.failing;
1+
package com.fasterxml.jackson.dataformat.xml.misc;
22

33
import com.fasterxml.jackson.annotation.JsonRootName;
44
import com.fasterxml.jackson.databind.DeserializationFeature;
55
import com.fasterxml.jackson.databind.SerializationFeature;
66
import com.fasterxml.jackson.dataformat.xml.*;
77
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
88

9-
public class Unwrapped374Test extends XmlTestBase
9+
public class RootNameWrapping374Test extends XmlTestBase
1010
{
1111
@JacksonXmlRootElement(localName = "Root")
1212
@JsonRootName("Root")

0 commit comments

Comments
 (0)