Skip to content

Commit 4f871d4

Browse files
bijukunjummenphilwebb
authored andcommitted
Fix Jaxb2TypeScanner to scan for @XmlRegistry
Update ClassPathJaxb2TypeScanner to scan for @XmlRegistry classes. Prior to this commit explicitly configured @XmlRegistry annotated classes were not registered with the JAXBContext when using the 'packagesToScan' property of the Jaxb2Unmarshaller. Issue: SPR-10714
1 parent 5a0e42b commit 4f871d4

File tree

5 files changed

+98
-3
lines changed

5 files changed

+98
-3
lines changed

spring-oxm/src/main/java/org/springframework/oxm/jaxb/ClassPathJaxb2TypeScanner.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.io.IOException;
2020
import java.util.ArrayList;
2121
import java.util.List;
22+
2223
import javax.xml.bind.annotation.XmlEnum;
24+
import javax.xml.bind.annotation.XmlRegistry;
2325
import javax.xml.bind.annotation.XmlRootElement;
2426
import javax.xml.bind.annotation.XmlSeeAlso;
2527
import javax.xml.bind.annotation.XmlType;
@@ -42,6 +44,7 @@
4244
* @author Arjen Poutsma
4345
* @author Juergen Hoeller
4446
* @author David Harrigan
47+
* @author Biju Kunjummen
4548
* @since 3.1.1
4649
* @see #scanPackages()
4750
*/
@@ -50,8 +53,11 @@ class ClassPathJaxb2TypeScanner {
5053
private static final String RESOURCE_PATTERN = "/**/*.class";
5154

5255
private static final TypeFilter[] JAXB2_TYPE_FILTERS = new TypeFilter[] {
53-
new AnnotationTypeFilter(XmlRootElement.class, false), new AnnotationTypeFilter(XmlType.class, false),
54-
new AnnotationTypeFilter(XmlSeeAlso.class, false), new AnnotationTypeFilter(XmlEnum.class, false)};
56+
new AnnotationTypeFilter(XmlRootElement.class, false),
57+
new AnnotationTypeFilter(XmlType.class, false),
58+
new AnnotationTypeFilter(XmlSeeAlso.class, false),
59+
new AnnotationTypeFilter(XmlEnum.class, false),
60+
new AnnotationTypeFilter(XmlRegistry.class, false)};
5561

5662

5763
private final ResourcePatternResolver resourcePatternResolver;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.oxm.jaxb;
18+
19+
import javax.xml.bind.annotation.XmlRootElement;
20+
21+
@XmlRootElement
22+
public class Airplane {
23+
24+
private String name;
25+
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
}

spring-oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@
5151
import org.xml.sax.ContentHandler;
5252
import org.xml.sax.Locator;
5353

54-
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
54+
5555
import static org.junit.Assert.*;
56+
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
5657
import static org.mockito.BDDMockito.*;
5758

59+
/**
60+
* @author Arjen Poutsma
61+
* @author Biju Kunjummen
62+
*/
5863
public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
5964

6065
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb.test";
@@ -281,6 +286,21 @@ public void marshalAttachments() throws Exception {
281286
verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
282287
}
283288

289+
@Test
290+
public void marshalAWrappedObjectHoldingAnXmlElementDeclElement() throws Exception {
291+
// SPR-10714
292+
marshaller = new Jaxb2Marshaller();
293+
marshaller.setPackagesToScan(new String[] { "org.springframework.oxm.jaxb" });
294+
marshaller.afterPropertiesSet();
295+
Airplane airplane = new Airplane();
296+
airplane.setName("test");
297+
StringWriter writer = new StringWriter();
298+
Result result = new StreamResult(writer);
299+
marshaller.marshal(airplane, result);
300+
assertXMLEqual("Marshalling should use root Element",
301+
writer.toString(), "<airplane><name>test</name></airplane>");
302+
}
303+
284304
@XmlRootElement
285305
@SuppressWarnings("unused")
286306
public static class DummyRootElement {

spring-oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2UnmarshallerTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
import static org.junit.Assert.*;
4040
import static org.mockito.BDDMockito.*;
4141

42+
/**
43+
* @author Arjen Poutsma
44+
* @author Biju Kunjummen
45+
*/
4246
public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests {
4347

4448
private static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
@@ -104,6 +108,7 @@ protected void testFlight(Object o) {
104108

105109
@Test
106110
@Override
111+
@SuppressWarnings("unchecked")
107112
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
108113
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
109114
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
@@ -115,5 +120,18 @@ public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
115120
testFlight(flight);
116121
}
117122

123+
@Test
124+
@SuppressWarnings("unchecked")
125+
public void unmarshalAnXmlReferingToAWrappedXmlElementDecl() throws Exception {
126+
// SPR-10714
127+
unmarshaller = new Jaxb2Marshaller();
128+
unmarshaller.setPackagesToScan(new String[] { "org.springframework.oxm.jaxb" });
129+
unmarshaller.afterPropertiesSet();
130+
Source source = new StreamSource(new StringReader(
131+
"<brand-airplane><name>test</name></brand-airplane>"));
132+
JAXBElement<Airplane> airplane = (JAXBElement<Airplane>) unmarshaller.unmarshal(source);
133+
assertEquals("Unmarshalling via explicit @XmlRegistry tag should return correct type",
134+
"test", airplane.getValue().getName());
135+
}
118136

119137
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
package org.springframework.oxm.jaxb;
3+
4+
import javax.xml.bind.JAXBElement;
5+
import javax.xml.bind.annotation.XmlElementDecl;
6+
import javax.xml.bind.annotation.XmlRegistry;
7+
import javax.xml.namespace.QName;
8+
9+
@XmlRegistry
10+
public class XmlRegObjectFactory {
11+
12+
@XmlElementDecl(name = "brand-airplane")
13+
public JAXBElement<Airplane> createAirplane(Airplane airplane) {
14+
return new JAXBElement<Airplane>(new QName("brand-airplane"), Airplane.class, null, airplane);
15+
}
16+
}

0 commit comments

Comments
 (0)