From 6b31ab01eaf234411503d781b42b7828d85a2cb6 Mon Sep 17 00:00:00 2001 From: Andriy Redko Date: Fri, 19 Jul 2024 17:18:09 -0400 Subject: [PATCH] CXF-9037: NPE when using com.ctc.wstx.returnNullForDefaultNamespace=true (#1962) * CXF-9037: NPE when using com.ctc.wstx.returnNullForDefaultNamespace=true * Address code review comments --- .../org/apache/cxf/staxutils/StaxSource.java | 5 +++ .../apache/cxf/staxutils/StaxUtilsTest.java | 37 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java b/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java index f74058406d8..2416d5596b9 100644 --- a/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java +++ b/core/src/main/java/org/apache/cxf/staxutils/StaxSource.java @@ -142,6 +142,11 @@ protected void parse() throws SAXException { if (nsUri == null) { nsUri = ""; } + // see please "com.ctc.wstx.returnNullForDefaultNamespace" property + if (nsPrefix == null) { + nsPrefix = ""; + } + contentHandler.startPrefixMapping(nsPrefix, nsUri); } contentHandler.startElement(uri == null ? "" : uri, localName, qname, getAttributes()); diff --git a/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java b/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java index 1e97337c754..d2272da7b4d 100644 --- a/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java +++ b/core/src/test/java/org/apache/cxf/staxutils/StaxUtilsTest.java @@ -25,6 +25,7 @@ import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilderFactory; @@ -47,11 +48,14 @@ import org.xml.sax.InputSource; +import com.ctc.wstx.stax.WstxInputFactory; + import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.helpers.IOUtils; import org.junit.Test; +import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertEquals; @@ -242,6 +246,39 @@ public void testEmptyNamespace() throws Exception { cycleString(testString); } + + @Test + public void testNullForDefaultNamespace() throws Exception { + final String bodyXml = "\n" + + "\n" + + " \n" + + " \n" + + " <<<<<\n" + + " \n" + + " \n" + + "\n"; + + try (ByteArrayInputStream in = new ByteArrayInputStream(bodyXml.getBytes(StandardCharsets.UTF_8))) { + final WstxInputFactory factory = new WstxInputFactory(); + factory.setProperty("com.ctc.wstx.returnNullForDefaultNamespace", "true"); + + final Source beforeSource = new StaxSource(factory.createXMLStreamReader(in)); + StringWriter writer = new StringWriter(); + StreamResult result = new StreamResult(writer); + + TransformerFactory tf = TransformerFactory.newInstance(); + + Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); + transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "Q{http://test.com/test}reqBody"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + + transformer.transform(beforeSource, result); + assertThat(writer.toString(), startsWith("")); + } + } + private void cycleString(String s) throws Exception { StringReader reader = new StringReader(s); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();