From 5b32f81f3170abce11b3201a1cc0cab314ba05f5 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 3 Jul 2021 18:41:18 -0700 Subject: [PATCH] Fix #482 (related to #481 and #480) for 2.12(.4) --- pom.xml | 9 ++++ release-notes/CREDITS-2.x | 5 ++ release-notes/VERSION-2.x | 3 ++ .../jackson/dataformat/xml/XmlFactory.java | 17 ++++++- .../interop/NonWoodstoxStaxImpl482Test.java | 49 +++++++++++++++++++ 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/fasterxml/jackson/dataformat/xml/interop/NonWoodstoxStaxImpl482Test.java diff --git a/pom.xml b/pom.xml index fd5479e5d..855b93f9e 100644 --- a/pom.xml +++ b/pom.xml @@ -109,6 +109,15 @@ alternative support for serializing POJOs as XML and deserializing XML as pojos. 2.3.2 test + + + com.sun.xml.stream + sjsxp + 1.0.2 + test + junit diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index ddd71bbcc..a8a9cbaf7 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -155,3 +155,8 @@ Westin Miller (westinrm@github) * Contributed #456: Fix JsonAlias with unwrapped lists (2.12.3) + +Tim Jacomb (timja@github) + +* Reported #482: Use of non-Stax2-compatible Stax2 implementation fails when reading + (2.12.4) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 0c226ccf2..28a1f18c8 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -11,6 +11,9 @@ Project: jackson-dataformat-xml #473: Parsing of `null` Integer fields changed behavior between versions 2.11.4 and 2.12.X (reported by Steviep@github) +#482: Use of non-Stax2-compatible Stax2 implementation fails when reading + from byte[] + (reported by Tim J) 2.12.3 (12-Apr-2021) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java index a2bee2187..b83c17f95 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java @@ -4,6 +4,7 @@ import javax.xml.stream.*; +import org.codehaus.stax2.XMLInputFactory2; import org.codehaus.stax2.io.Stax2ByteArraySource; import org.codehaus.stax2.io.Stax2CharArraySource; @@ -610,7 +611,13 @@ protected FromXmlParser _createParser(char[] data, int offset, int len, IOContex // is always same as if 'false' was passed XMLStreamReader sr; try { - sr = _xmlInputFactory.createXMLStreamReader(new Stax2CharArraySource(data, offset, len)); + // 03-Jul-2021, tatu: [dataformat-xml#482] non-Stax2 impls unlikely to + // support so avoid: + if (_xmlInputFactory instanceof XMLInputFactory2) { + sr = _xmlInputFactory.createXMLStreamReader(new Stax2CharArraySource(data, offset, len)); + } else { + sr = _xmlInputFactory.createXMLStreamReader(new CharArrayReader(data, offset, len)); + } } catch (XMLStreamException e) { return StaxUtil.throwAsParseException(e, null); } @@ -628,7 +635,13 @@ protected FromXmlParser _createParser(byte[] data, int offset, int len, IOContex { XMLStreamReader sr; try { - sr = _xmlInputFactory.createXMLStreamReader(new Stax2ByteArraySource(data, offset, len)); + // 03-Jul-2021, tatu: [dataformat-xml#482] non-Stax2 impls unlikely to + // support so avoid: + if (_xmlInputFactory instanceof XMLInputFactory2) { + sr = _xmlInputFactory.createXMLStreamReader(new Stax2ByteArraySource(data, offset, len)); + } else { + sr = _xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(data, offset, len)); + } } catch (XMLStreamException e) { return StaxUtil.throwAsParseException(e, null); } diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/interop/NonWoodstoxStaxImpl482Test.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/interop/NonWoodstoxStaxImpl482Test.java new file mode 100644 index 000000000..8c5e14062 --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/interop/NonWoodstoxStaxImpl482Test.java @@ -0,0 +1,49 @@ +package com.fasterxml.jackson.dataformat.xml.interop; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.dataformat.xml.XmlFactory; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; +import com.sun.xml.stream.ZephyrParserFactory; +import com.sun.xml.stream.ZephyrWriterFactory; + +// to verify issue behind [dataformat-xml#482] +public class NonWoodstoxStaxImpl482Test extends XmlTestBase +{ + static class Root { + public int value = 3; + } + + private final XmlMapper SJSXP_MAPPER = XmlMapper.builder( + XmlFactory.builder() + .inputFactory(new ZephyrParserFactory()) + .outputFactory(new ZephyrWriterFactory()) + .build()) + .build(); + + // [dataformat-xml#482] + public void testSjsxpFromByteArray() throws Exception + { + byte[] xml0 = SJSXP_MAPPER.writeValueAsBytes(new Root()); + // and just for fun, ensure offset handling works: + byte[] xml = new byte[xml0.length + 10]; + System.arraycopy(xml0, 0, xml, 5, xml0.length); + Root result = SJSXP_MAPPER.readValue(xml, 5, xml0.length, Root.class); + assertNotNull(result); + } + + // [dataformat-xml#482] + public void testSjsxpFromCharArray() throws Exception + { + char[] xml0 = SJSXP_MAPPER.writeValueAsString(new Root()).toCharArray(); + // add offset + char[] xml = new char[xml0.length + 10]; + System.arraycopy(xml0, 0, xml, 5, xml0.length); + ObjectReader r = SJSXP_MAPPER.readerFor(Root.class); + JsonParser p = r.createParser(xml, 5, xml0.length); + Root result = r.readValue(p); + p.close(); + assertNotNull(result); + } +}