Skip to content

Addition of default namespace into the element when there is already a namespace declared #242

@BerksanAtes

Description

@BerksanAtes

Context

When an element created with a given prefix is added to the SOAPBody default namespace is added beside the namespace including the prefix. This happens only with Java 9+. For Java 8, it works without a problem. Same behavior is observed from SAAJ version 1.4.0 to SAAJ 3.0.

output sample when run with java 8:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">SOAP-ENV:Header/SOAP-ENV:Body<ns0:testNamespace xmlns:ns0="http://objects.get.echo.io"></ns0:testNamespace></SOAP-ENV:Body></SOAP-ENV:Envelope>

output sample when run with java 9+:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">SOAP-ENV:Header/SOAP-ENV:Body<ns0:testNamespace xmlns="http://objects.get.echo.io" xmlns:ns0="http://objects.get.echo.io"></ns0:testNamespace></SOAP-ENV:Body></SOAP-ENV:Envelope>

Analysis

In com.sun.xml.messaging.saaj.soap.impl.ElementImpl class addNode method, after Node is casted to ElementImpl to perform getElementQName() call, QName is created without prefix. This behavior leads to addition of default namespace since search for alreadyDeclaredUri in getNamespaceURI(prefix) return null in ensureNamespaceIsDeclared method.

Reproducing the issue

        Running this code with java 9+ and SAAJ 1.4.0+ should create the issue. 
            System.setProperty("javax.xml.soap.SAAJMetaFactory", "com.sun.xml.messaging.saaj.soap.SAAJMetaFactoryImpl");
            MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
            SOAPMessage request = mf.createMessage();
            SOAPPart part = request.getSOAPPart();
            SOAPEnvelope envelope = part.getEnvelope();
            SOAPBody body = envelope.getBody();
            SOAPFactory SOAP_FACTORY = SOAPFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
            SOAPElement testElement = SOAP_FACTORY.createElement("testNamespace", "ns0", "http://objects.get.echo.io"); // <- A soap element is created with namespace prefix ns0
            SOAPElement inElement = SOAP_FACTORY.createElement("in", null, "");
            testElement.addChildElement(inElement);
            body.addChildElement(testElement); // <- default namespace is added beside namespace with ns0 prefix

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions