A WSO2-ESB Service is recieving an 'empty' XML response from an endpoint in some cases.
Usually the response looks like this. It contains several result
s:
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:results xmlns:ns2="http://the-endpoint.com/">
<result>some result</result>
<result>some other result</result>
<result>some very different result</result>
</ns2:results>
</S:Body>
</S:Envelope>
which is then translated to an array of result
s and consumed by clients.
{"results": ["some result", ...]}
But in some rare cases the endpoints response is just an empty list of results which is translated to an empty <ns2:results/>
tag by JAX-WS appearently.
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:results xmlns:ns2="http://the-endpoint.com/"/>
</S:Body>
</S:Envelope>
WSO2-ESB then translates this to a null
object, which is causing trouble.
{"results": null}
Now how can I make the WSO2-ESB Webservice return the empty list as {"results": []}
in case there is an empty response?
You need to add XML processing instructions to the outer element using XSLT mediator.
You can refer to this blog on adding processing instruction. https://medium.com/@CKoutsoumpos/wso2-esb-handle-empty-or-single-item-arrays-during-json-transformation-with-xslt-c1d5a778a548