Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of non-Stax2-compatible Stax2 implementation fails when reading from byte[] #482

Closed
cowtowncoder opened this issue Jul 4, 2021 · 1 comment
Milestone

Comments

@cowtowncoder
Copy link
Member

(note: inspired by #481)

Looks like there is one error mode when using JDK-bundled Stax implementation -- or more generally, anything that does not implement Stax2 extensions. Exception when reading from byte[] looks something like this:

java.lang.UnsupportedOperationException: Cannot create XMLStreamReader or XMLEventReader from a org.codehaus.stax2.io.Stax2ByteArraySource
	at com.sun.xml.stream.ZephyrParserFactory.jaxpSourcetoXMLInputSource(ZephyrParserFactory.java:304)
	at com.sun.xml.stream.ZephyrParserFactory.createXMLStreamReader(ZephyrParserFactory.java:151)
	at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:631)
	at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:1)
	at com.fasterxml.jackson.core.JsonFactory.createParser(JsonFactory.java:1122)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3636)	

It should be relatively easy to work around the issue, to provide input source non-Stax2 implementations can use instead.

@timja
Copy link

timja commented Jul 4, 2021

Thanks!

I verified this fixed our test (I cherry-picked the fix into 2.12):

https://github.com/jenkinsci/jackson2-api-plugin/blob/master/src/test/java/com/fasterxml/jackson/dataformat/xml/XmlMapperTest.java

cc @jglick @basil

diff --git a/pom.xml b/pom.xml
index 4eba2d1..500be94 100644
--- a/pom.xml
+++ b/pom.xml
@@ -67,7 +67,7 @@
     <revision>2.12.4</revision>
     <changelist>-SNAPSHOT</changelist>
     <java.level>8</java.level>
-    <jenkins.version>2.222.4</jenkins.version>
+    <jenkins.version>2.300</jenkins.version>
     <jackson.version>2.12.3</jackson.version>
     <jackson-databind.version>${jackson.version}</jackson-databind.version>
   </properties>
@@ -149,7 +149,7 @@
     <dependency>
       <groupId>com.fasterxml.jackson.dataformat</groupId>
       <artifactId>jackson-dataformat-xml</artifactId>
-      <version>${jackson.version}</version>
+      <version>2.12.4-SNAPSHOT</version>
     </dependency>

     <dependency>
diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/XmlMapperTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/XmlMapperTest.java
index a864ecd..825d5a4 100644
--- a/src/test/java/com/fasterxml/jackson/dataformat/xml/XmlMapperTest.java
+++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/XmlMapperTest.java
@@ -10,8 +10,6 @@ import org.jvnet.hudson.test.RealJenkinsRule;

 import java.nio.charset.StandardCharsets;

-import javax.xml.stream.XMLInputFactory;
-
 public class XmlMapperTest {

     @Rule public RealJenkinsRule rr = new RealJenkinsRule();
@@ -22,11 +20,7 @@ public class XmlMapperTest {
     }

     private static void _smokes(JenkinsRule r) throws Throwable {
-        XMLInputFactory inputFactory =
-                XMLInputFactory.newFactory(
-                        XMLInputFactory.class.getName(), XmlFactory.class.getClassLoader());
-        XmlFactory factory = new XmlFactory(inputFactory);
-        XmlMapper mapper = new XmlMapper(factory);
+        XmlMapper mapper = new XmlMapper();
         String content = "<foo><bar><id>123</id></bar></foo>";
         Foo foo = mapper.readValue(content.getBytes(StandardCharsets.UTF_8), Foo.class);
         assertNotNull(foo.getBar());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants