wso2wso2-enterprise-integratorapache-synapsewso2-esb

Is there a way to set a default value for a property mediator if the property does not exist in the request body?


I am building a simple API in WSO2 EI/ESB. I am saving each of the request parameters to properties like so:

<property expression="json-eval($.client_id)" name="client_id" scope="default" type="STRING"/>

And then building a payload using the payload factory:

<payloadFactory media-type="json">
                <format>       
                    {

                        "req_type": "1",
                        "client_id": $1

                    }
                </format>
                <args>
                    <arg evaluator="xml" expression="get-property('client_id')"/>
                </args>
</payloadFactory>

However, if I sent an empty message {} (without the client_id) then nothing is placed in the $1 argument and the resulting payload will not validate:

{
                    "req_type": "1",
                    "client_id:
}

What I would like to know is if there is a way to set a default value when saving this expression in the property mediator? For example: <property expression="json-eval($.client_id)" name="client_id" scope="default" type="STRING" defaultValue="0"/> or something like that.

I am aware I can implement filters to check whether the field exists and validate it but I find that can become a bit cluttered.


Solution

  • No you can't using payloadFactory. You have to choose from different solutions: