This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/main by this push:
     new 6b31ab01ea CXF-9037: NPE when using 
com.ctc.wstx.returnNullForDefaultNamespace=true (#1962)
6b31ab01ea is described below

commit 6b31ab01eaf234411503d781b42b7828d85a2cb6
Author: Andriy Redko <[email protected]>
AuthorDate: Fri Jul 19 17:18:09 2024 -0400

    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
---
 .../java/org/apache/cxf/staxutils/StaxSource.java  |  5 +++
 .../org/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 f74058406d..2416d5596b 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 @@ public class StaxSource extends SAXSource implements 
XMLReader {
                         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 1e97337c75..d2272da7b4 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.InputStream;
 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.w3c.dom.Element;
 
 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 class StaxUtilsTest {
         cycleString(testString);
     }
 
+    
+    @Test
+    public void testNullForDefaultNamespace() throws Exception {
+        final String bodyXml = "<?xml version=\"1.0\"?>\n"
+            + "<soap:Envelope 
xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\";>\n"
+            + "  <soap:Body>\n"
+            + "    <testReq xmlns=\"http://test.com/test\";>\n"
+            + "        <reqBody>&lt;&lt;&lt;&lt;&lt;</reqBody>\n"
+            + "    </testReq>\n"
+            + "  </soap:Body>\n"
+            + "</soap:Envelope>\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("<?xml version=\"1.0\" 
encoding=\"UTF-8\" standalone=\"yes\"?>"));
+        }
+    }
+
     private void cycleString(String s) throws Exception {
         StringReader reader = new StringReader(s);
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

Reply via email to