tefra / xsdata

Naive XML & JSON Bindings for python
https://xsdata.readthedocs.io
MIT License
334 stars 61 forks source link

Automatic Namespace Prefix Generation Causing XML Schema Validation Failure #851

Closed antoniospneto closed 11 months ago

antoniospneto commented 1 year ago

I am currently experimenting with WSDL communication using XSDATA, and a specific issue has arisen: Is there any way to disable the automatic generation of the namespace prefix in XML tags? We are facing a situation where the webservice rejects the service due to a failure in XML schema validation caused by automatically generated namespace prefixes.

Here's an example of the payload generated by XSDATA (which is rejected by the webservice):

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soapenvelope">
    <soapenv:Body>
        <ns0:nfeDadosMsg xmlns:ns0="http://www.portalfiscal.inf.br/nfe/wsdl/NFeStatusServico4">
            <ns1:consStatServ xmlns:ns1="http://www.portalfiscal.inf.br/nfe" versao="4.00">
                <ns1:tpAmb>1</ns1:tpAmb>
                <ns1:cUF>35</ns1:cUF>
                <ns1:xServ>STATUS</ns1:xServ>
            </ns1:consStatServ>
        </ns0:nfeDadosMsg>
    </soapenv:Body>
</soapenv:Envelope>

And here is an example of how it should be (this is successfully processed by the webservice):

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soapenvelope">
    <soapenv:Body>
        <ns0:nfeDadosMsg xmlns:ns0="http://www.portalfiscal.inf.br/nfe/wsdl/NFeStatusServico4">
            <consStatServ xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
                <tpAmb>1</tpAmb>
                <cUF>35</cUF>
                <xServ>STATUS</xServ>
            </consStatServ>
        </ns0:nfeDadosMsg>
    </soapenv:Body>
</soapenv:Envelope>

Note that the content of consStatServis now without a prefix. The webservice in question is the Brazilian electronic invoice issuance service, available at https://www.nfe.fazenda.gov.br/.

tefra commented 1 year ago

Interesting case, it doesn't like the ns1 on the consStatServ but it's fine with the ns0 for the nfeDadosMsg element. Their server implementation is... not pretty :)

That's just impossible to implement in the client side. Unless if we make the http://www.portalfiscal.inf.br/nfe the default namespace, can you try something like this?

    ns_map = {
        None: "http://www.portalfiscal.inf.br/nfe",
    }
    result = xml_serializer.render(obj, ns_map=ns_map)
tefra commented 11 months ago

I am assuming the tip above worked...