Jason Lee
Jason Lee

Reputation: 48

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.

Upvotes: 2

Views: 1703

Answers (3)

simar
simar

Reputation: 1832

Conditional xpath ? This might work. Play around with code underneath.

<enrich>
    <source clone="true" type="custom" xpath="0"  />
    <target action="replace" type="custom" xpath="" property="boolean(string-length(//client_id) != 0)" />
</enrich>

Upvotes: 0

Lo&#239;c
Lo&#239;c

Reputation: 88

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

  • Replace your payloadFactory with XSLT transformation which is way more flexible (best solution)
  • Add a filter to build your parameter and set a value for default
  • Use a script mediator to replace the painful filter and be able to manage if/then/else statements easily. You can then initialize the property from the script mediator.

Upvotes: 2

Bee
Bee

Reputation: 12512

I don't think there is a way to do that. You will have to use filters.

Upvotes: 0

Related Questions