Mule 4 is new and there are many problems, it takes a lot of time for us to get familiar, so we know it at all. One of the problems that I’ve had recently with Mule 4 is how to change the default value of XML Reader Properties “nullValueOn” for DataWeave from “blank” to “none”.
If you know the “nullValueOn” attribute, this attribute is used when we transform data input as XML to any output using DataWeave. The content of our input XML data can have tags no appear, empty or blank. With Mule 3, the default value of “nullValueOn” is “none”, meaning that only when input XML data does not have the tag we want, DataWeave 1 will treat the tag as null. But in Mule 4, because the default value of this attribute is “blank”, meaning that the tags have no value, only the tag name, such as <id />, DataWeave 2 will also treat the tag with value as null. This may seem unreasonable because in this case, this tag still exists in the input data. But in terms of meaning, I also feel reasonable, because if the value is blank, then what is the purpose to output that tag? …
For you to better understand, I have an application written in Mule 3 as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd"> <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration" /> <flow name="testFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP" /> <set-payload value="<st:student xmlns:st="http://huongdanjava.com/student/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://huongdanjava.com/student/ student.xsd "><id/><name>Khanh</name><address>Viet Nam</address></st:student>" mimeType="application/xml" doc:name="Set Payload" /> <dw:transform-message doc:name="Transform Message"> <dw:set-payload> <![CDATA[ %dw 1.0 %output application/xml writeNilOnNull=true %namespace ns0 http://huongdanjava.com/student/ --- { ns0#student: { id: payload.ns0#student.id, name: payload.ns0#student.name, address: payload.ns0#student.address } } ]]> </dw:set-payload> </dw:transform-message> </flow> </mule> |
This same application is written in Mule 4 as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd"> <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="442c5e6a-f3dd-4a25-a1e2-ace11dd562d1"> <http:listener-connection host="0.0.0.0" port="8081" /> </http:listener-config> <flow name="testFlow" doc:id="8ad12d74-a3c9-4527-bfa0-395c4c66a926"> <http:listener doc:name="Listener" doc:id="e603cbe1-a9b0-484d-bf54-9d1ca5af6898" config-ref="HTTP_Listener_config" path="/test" /> <set-payload value='<st:student xmlns:st="http://huongdanjava.com/student/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://huongdanjava.com/student/ student.xsd "><id/><name>Khanh</name><address>Viet Nam</address></st:student>' doc:name="Set Payload" doc:id="144b8106-24f3-427a-8142-2307f6fc7acd" mimeType="application/xml" /> <ee:transform doc:name="Transform Message" doc:id="8f455336-2b94-409a-b95f-1c1eec709149"> <ee:message> <ee:set-payload> <![CDATA[ %dw 2.0 output application/xml writeNilOnNull=true ns ns0 http://huongdanjava.com/student/ --- { ns0#student: { id: payload.ns0#student.id, name: payload.ns0#student.name, address: payload.ns0#student.address } } ]]> </ee:set-payload> </ee:message> </ee:transform> </flow> </mule> |
Both versions of this application have an HTTP Listener endpoint, a Set Payload endpoint with the following content:
1 2 3 4 5 |
<st:student xmlns:st="http://huongdanjava.com/student/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://huongdanjava.com/student/ student.xsd "> <id/> <name>Khanh</name> <address>Viet Nam</address> </st:student> |
and a Transform Message endpoint.
The result when we request to http://localhost:8081/test of Mule 3 will be as follows:
And Mule 4 will be:
Can you see the difference? Mule 4 is considering the <id> tag in the input data as null, then should output an XML tag with nil = true.
We can change the default value of the “nullValueOn” attribute as well as other XML Reader Properties in Mule 3 and Mule 4. But the configuration of this attribute as well as other properties will be different for Mule 3 and Mule 4. Details:
For Mule 3, in the content of the <dw:transform-message> tag, you can add the <dw:input-payload> tag if your application does not already exist this tag, then in this <dw:input-payload> tag, add the <dw:reader-property> tag with name as the XML Reader Properties name which you want to change the default value. For example, I can configure the “nullValueOn” attribute for my application on Mule 3 as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<dw:transform-message doc:name="Transform Message"> <dw:input-payload> <dw:reader-property name="nullValueOn" value="blank" /> </dw:input-payload> <dw:set-payload> <![CDATA[ %dw 1.0 %output application/xml writeNilOnNull=true %namespace ns0 http://huongdanjava.com/student/ --- { ns0#student: { id: payload.ns0#student.id, name: payload.ns0#student.name, address: payload.ns0#student.address } } ]]> </dw:set-payload> </dw:transform-message> |
Requesting the application again, you will see the result as above Mule 4.
For Mule 4, the configuration is completely different.
Module 4 relies on the MIME Type of the input data object and we will configure the XML Reader Properties use this MIME Type. This means, if I want to change the default value of an input data, I need to change the MIME Type of that input data. For example, Mule 4 example above, my input data is Payload, its current MIME Type is application/xml, what we need to do is change this MIME Type to add the XML Reader Properties to it.
You can click on Set Payload endpoint and open the Set Payload tab:
Then, click on the MIME Type tab in this window and then add Parameter as follows:
XML content of Set Payload endpoint now will be:
1 2 3 |
<set-payload value='<st:student xmlns:st="http://huongdanjava.com/student/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://huongdanjava.com/student/ student.xsd "><id/><name>Khanh</name><address>Viet Nam</address></st:student>' doc:name="Set Payload" doc:id="144b8106-24f3-427a-8142-2307f6fc7acd" mimeType="application/xml; nullvalueon=none" /> |
You can see that the MIME Type of Set Payload endpoint has been configured to add XML Reader Properties “nullValueOn” with “application/xml; nullvalueon=none”.
Run the example again to see the results! …
One thing to note is that the configuration of the XML Reader Properties can also apply to other MIME Type such as application/json!