Skip to content

Commit e641ae4

Browse files
committed
Fix #637: merge namespace information properly
1 parent ac00d64 commit e641ae4

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project: jackson-dataformat-xml
1616
`XmlMapper.createParser(XMLStreamReader)` overloads
1717
#634: Support use of xsi:type for polymorphic deserialization
1818
(FromXmlParser.Feature.AUTO_DETECT_XSI_TYPE)
19+
#637: `JacksonXmlAnnotationIntrospector.findNamespace()` should
20+
properly merge namespace information
1921
* Upgrade Woodstox to 6.6.0 (latest at the time)
2022

2123
2.16.1 (24-Dec-2023)

src/main/java/com/fasterxml/jackson/dataformat/xml/JacksonXmlAnnotationIntrospector.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,27 @@ public PropertyName findRootName(AnnotatedClass ac)
117117
@Override
118118
public String findNamespace(MapperConfig<?> config, Annotated ann)
119119
{
120-
JacksonXmlProperty prop = _findAnnotation(ann, JacksonXmlProperty.class);
121-
if (prop != null) {
122-
return prop.namespace();
120+
String ns1 = null;
121+
JacksonXmlProperty xmlProp = _findAnnotation(ann, JacksonXmlProperty.class);
122+
if (xmlProp != null) {
123+
ns1 = xmlProp.namespace();
123124
}
124125
// 14-Nov-2020, tatu: 2.12 adds namespace for this too
125126
JsonProperty jprop = _findAnnotation(ann, JsonProperty.class);
127+
String ns2 = null;
126128
if (jprop != null) {
127-
return jprop.namespace();
129+
ns2 = jprop.namespace();
128130
}
129-
return null;
131+
if (ns1 == null) {
132+
return ns2;
133+
}
134+
if (ns2 == null) {
135+
return ns2;
136+
}
137+
if (ns1.isEmpty()) {
138+
return ns2;
139+
}
140+
return ns1;
130141
}
131142

132143
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fasterxml.jackson.dataformat.xml.ser;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
5+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
6+
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
7+
8+
// [dataformat-xml#637]
9+
public class SerializationNameMergingTest extends XmlTestBase
10+
{
11+
// [dataformat-xml#637]
12+
static class NamesBean {
13+
// XML annotations have precedence over default/standard/json ones
14+
// but local name, namespace should be merged
15+
@JsonProperty(value="value", namespace="uri:ns1")
16+
@JacksonXmlProperty(isAttribute=true)
17+
public int valueDefault = 42;
18+
}
19+
20+
private final XmlMapper MAPPER = newMapper();
21+
22+
23+
// [dataformat-xml#637]
24+
public void testNamespaceMerging637() throws Exception
25+
{
26+
assertEquals(a2q("<NamesBean xmlns:wstxns1='uri:ns1' wstxns1:value='42'/>"),
27+
MAPPER.writeValueAsString(new NamesBean()));
28+
}
29+
}

0 commit comments

Comments
 (0)