1

Actually I'm trying to get an empty value when I set a Property in a sequence in WSO2 ESB with an empty string. I have tried many things but always get the result "null" or "\"\"" instead of "" when I get the property, here is my code:

<property value="&quot;&quot;"
name="arq.general.DestinationSystem" scope="default"
type="STRING" xmlns:ns="http://org.apache.synapse/xsd" 
xmlns:ns2="http://org.apache.synapse/xsd"/>
<property name="arq.general.ParentInstanceID" scope="default"
type="STRING" value=""/>
<property expression="get-property('NonExistentProperty')" 
name="arq.functional.User"
scope="default" type="STRING"
xmlns:ns="http://org.apache.synapse/xsd" 
xmlns:ns2="http://org.apache.synapse/xsd"/>

Please could you help?

Cheers,

Tony

++ the payload Factory:

<payloadFactory media-type="xml">
            <format>
                <MensajeAuditoria xmlns="">
                    <Timestamp>$1</Timestamp>
                    <TrackingID>$2</TrackingID>
                    <SourceApplication>$3</SourceApplication>
                    <OperationName>$4</OperationName>
                    <ParentInstanceID>$5</ParentInstanceID>
                    <InstanceID>$6</InstanceID>
                    <ServiceID>$7</ServiceID>
                    <FunctionalID>$8</FunctionalID>
                    <AdapterType>$9</AdapterType>
                    <AdapterPoint>$10</AdapterPoint>
                    <HostName>$11</HostName>
                    <User>$12</User>
                </MensajeAuditoria>
            </format>
            <args>
                <arg evaluator="xml" expression="get-property('SYSTEM_TIME')"/>
                <arg evaluator="xml" expression="get-property('arq.general.TrackingID')"/>
                <arg evaluator="xml" expression="get-property('arq.general.SourceApplication')"/>
                <arg evaluator="xml" expression="get-property('arq.functional.OperationName')"/>
                <arg evaluator="xml" expression="get-property('arq.general.ParentInstanceID')"/>
                <arg evaluator="xml" expression="get-property('arq.general.InstanceID')"/>
                <arg evaluator="xml" expression="get-property('arq.general.ServiceID')"/>
                <arg evaluator="xml" expression="get-property('arq.functional.FunctionalID')"/>
                <arg evaluator="xml" expression="get-property('arq.general.AdapterType')"/>
                <arg evaluator="xml" expression="$func:AdapterPoint"/>
                <arg evaluator="xml" expression="get-property('SERVER_IP')"/>
                <arg evaluator="xml" expression="get-property('arq.functional.User')"/>

            </args>
        </payloadFactory>
    </else>
</filter>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>

Adding the factory result:

{
"MensajeAuditoria": {
    "Timestamp": 1492777451830,
    "TrackingID": "76b9858d-8421-4d7e-d2af-e8e411382e2e",
    "SourceApplication": "API Manager",
    "OperationName": null,
    "ParentInstanceID": null,
    "InstanceID": "76b9858d-8421-4d7e-d2af-e8e411382e2e",
    "ServiceID": "PRX_PROY1_AEX_AltaCliente",
    "FunctionalID": null,
    "AdapterType": "AEXP",
    "AdapterPoint": "PreActRequest",
    "HostName": "172.16.3.97",
    "User": null,
}}
Community
  • 1
  • 1
Tony Montana
  • 36
  • 1
  • 4
  • Could you tell me which version of the ESB you are using, and also what the output is for your ParentInstanceID property. If I create a property like that and then log it I get an empty string in my log, and none of the results that you show. – ophychius Apr 21 '17 at 07:09
  • Hi, I'm using WSO2 ESB 5.0.0, it happens when I send it through JMS, the JSON message that I receive is with null values, instead of "" when I assign them with a payloadFactory. – Tony Montana Apr 21 '17 at 08:14
  • Can you show your payload factory? And if they are not empty properties are they supposed to hold strings, or ints for example? – ophychius Apr 21 '17 at 08:20
  • Just added on the main post. – Tony Montana Apr 21 '17 at 12:16

4 Answers4

1

An other solution is to use "string" xpath function :

<property name="arq.general.ParentInstanceID" expression="string('')"/>
Jean-Michel
  • 5,926
  • 1
  • 13
  • 19
  • Thanks for the reply, in this case it didn't work either. It only worked whith the JavaScript script. I think the payloadFactory makes some conversion from XML to JSON that replaces "" value with null value. – Tony Montana Apr 21 '17 at 12:32
0

I have achieved a work-around using JavaScript but it should be possible to set it in the property mediator, or may be with enrich.

<script language="js"><![CDATA[var payload = mc.getPayloadJSON(); 
if(payload.MensajeAuditoria.ParentInstanceID== null){
    payload.MensajeAuditoria.ParentInstanceID="";    
}
mc.setPayloadJSON(payload);
mc.setProperty("MyProperty","");]]></script>

If you get an XML solution, please let me know.

Thanks.

Tony Montana
  • 36
  • 1
  • 4
0

I have tried your payloadFactory and it turns out it works like a charm on 4.8.1 but it fails on 5.0.0. This is most likely a change in the JSON message builder that they use in the ESB. The problem is not on your empty property but in the automatic translation from XML to JSON that happens when you set the messageType property.

What you can do to solve this though is the make the payload mediator create json straight away as follows:

<payloadFactory media-type="json">
            <format>
               {"MensajeAuditoria":{
                    "Timestamp":$1,
                    "TrackingID":"$2"
                    ...
                }
               }

            </format>
            <args>
                <arg evaluator="xml" expression="get-property('SYSTEM_TIME')"/>
                <arg evaluator="xml" expression="get-property('arq.general.TrackingID')"/>
                ...
            </args>
        </payloadFactory>
    </else>
</filter>

This way you are in control of the quotes, and it will never put null there unless you actually want it there.

Hope this helps solve your problem

ophychius
  • 2,623
  • 2
  • 27
  • 49
  • Great!!!! Thank you very much!!! I marked your response as correct but I have this new account and it doesn't get marked till i have 15 points of reputation :( . Thank you anyway. – Tony Montana Apr 24 '17 at 09:33
0
Good thing would be to use enrich mediator to map the json field to xml field if there is a vale for the incoming field.
Sample
      <filter regex="true" source="boolean(get-property('START_DATE'))">
        <then>
          <enrich description="Add startDate tag">
            <source clone="true" type="inline">
              <org:startDate xmlns:org="urn:example.com/service/org"/>
            </source>
            <target action="child" xpath="//*[local-name()=get-property('RequestType')]"/>
          </enrich>
          <enrich description="populate startDate">
            <source clone="true" property="START_DATE" type="property"/>
            <target
              xmlns:org="urn:example.com/servi`enter code here`ce/org" xpath="//org:startDate"/>
          </enrich>
        </then>
        <else/>
      </filter>