Skip to content

Commit 8ffed5c

Browse files
committed
Fix #117
1 parent 5b52483 commit 8ffed5c

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

release-notes/VERSION

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Project: jackson-dataformat-xml
22
Version: 2.4.1 (xx-Jun-2014)
33

4+
#117: @JsonAnyGetter + @JsonTypeInfo combination prevents serialization of properties as elements
5+
(reported by gawi@github)
6+
47
------------------------------------------------------------------------
58
=== History: ===
69
------------------------------------------------------------------------

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ protected void serializeFields(Object bean, JsonGenerator jgen0, SerializerProvi
145145
}
146146

147147
final int attrCount = _attributeCount;
148-
boolean isAttribute = xgen._nextIsAttribute;
148+
final boolean isAttribute = xgen._nextIsAttribute;
149149
if (attrCount > 0) {
150150
xgen.setNextIsAttribute(true);
151151
}
@@ -176,6 +176,9 @@ protected void serializeFields(Object bean, JsonGenerator jgen0, SerializerProvi
176176
}
177177
}
178178
if (_anyGetterWriter != null) {
179+
// For [#117]: not a clean fix, but with @JsonTypeInfo, we'll end up
180+
// with accidental attributes otherwise
181+
xgen.setNextIsAttribute(false);
179182
_anyGetterWriter.getAndSerialize(bean, xgen, provider);
180183
}
181184
} catch (Exception e) {
@@ -215,6 +218,7 @@ protected void serializeFieldsFiltered(Object bean, JsonGenerator jgen0,
215218
return;
216219
}
217220

221+
final boolean isAttribute = xgen._nextIsAttribute;
218222
final int attrCount = _attributeCount;
219223
if (attrCount > 0) {
220224
xgen.setNextIsAttribute(true);
@@ -225,7 +229,9 @@ protected void serializeFieldsFiltered(Object bean, JsonGenerator jgen0,
225229
int i = 0;
226230
try {
227231
for (final int len = props.length; i < len; ++i) {
228-
if (i == attrCount) {
232+
// 28-jan-2014, pascal: we don't want to reset the attribute flag if we are an unwrapping serializer
233+
// that started with nextIsAttribute to true because all properties should be unwrapped as attributes too.
234+
if (i == attrCount && !(isAttribute && isUnwrappingSerializer())) {
229235
xgen.setNextIsAttribute(false);
230236
}
231237
// also: if this is property to write as text ("unwrap"), need to:
@@ -239,6 +245,9 @@ protected void serializeFieldsFiltered(Object bean, JsonGenerator jgen0,
239245
}
240246
}
241247
if (_anyGetterWriter != null) {
248+
// For [#117]: not a clean fix, but with @JsonTypeInfo, we'll end up
249+
// with accidental attributes otherwise
250+
xgen.setNextIsAttribute(false);
242251
_anyGetterWriter.getAndSerialize(bean, xgen, provider);
243252
}
244253
} catch (Exception e) {
@@ -261,7 +270,6 @@ public void serializeWithType(Object bean, JsonGenerator jgen, SerializerProvide
261270
_serializeWithObjectId(bean, jgen, provider, typeSer);
262271
return;
263272
}
264-
265273
/* Ok: let's serialize type id as attribute, but if (and only if!)
266274
* we are using AS_PROPERTY
267275
*/

src/test/java/com/fasterxml/jackson/dataformat/xml/XmlTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ protected byte[] utf8Bytes(String str) {
265265

266266
/**
267267
* Helper method that tries to remove unnecessary namespace
268-
* declaration that default JDK XML parser (SJSXP) seems fit
268+
* declaration that default JDK XML parser (SJSXP) sees fit
269269
* to add.
270270
*/
271271
protected static String removeSjsxpNamespace(String xml)

src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestSerializationAttr.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.fasterxml.jackson.dataformat.xml.ser;
22

33
import java.io.IOException;
4+
import java.util.*;
45

5-
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.annotation.*;
67

78
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
89
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
@@ -35,6 +36,22 @@ public class Jurisdiction {
3536
@JacksonXmlProperty(isAttribute=true)
3637
protected int value = 13;
3738
}
39+
40+
@JacksonXmlRootElement(localName = "dynaBean", namespace = "")
41+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "class", include = JsonTypeInfo.As.PROPERTY)
42+
public class DynaBean {
43+
private final Map<String, String> _properties = new TreeMap<String, String>();
44+
45+
public DynaBean(Map<String, String> values) {
46+
_properties.putAll(values);
47+
}
48+
49+
@JsonAnyGetter
50+
@JacksonXmlProperty(isAttribute = false)
51+
public Map<String, String> getProperties() {
52+
return _properties;
53+
}
54+
}
3855

3956
/*
4057
/**********************************************************
@@ -81,6 +98,16 @@ public void testIssue6() throws IOException
8198
assertEquals("<Jurisdiction name=\"Foo\" value=\"13\"/>",
8299
_xmlMapper.writeValueAsString(new Jurisdiction()));
83100
}
101+
102+
public void testIssue117AnySetterAttrs() throws IOException
103+
{
104+
Map<String, String> values = new HashMap<String, String>();
105+
values.put("prop1", "val1");
106+
107+
String xml = _xmlMapper.writeValueAsString(new DynaBean(values));
108+
assertEquals("<dynaBean class=\"TestSerializationAttr$DynaBean\"><prop1>val1</prop1></dynaBean>",
109+
removeSjsxpNamespace(xml));
110+
}
84111

85112
/*
86113
/**********************************************************

0 commit comments

Comments
 (0)