2

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.

Community
  • 1
  • 1
Jason Lee
  • 48
  • 6

3 Answers3

2

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.
Loïc
  • 88
  • 8
  • Thanks! Yeah, I was a bit doubtful that there would be a way to do it, just thought I should ask the community. I will look at using the XSLT transformation or a script mediator, thanks for the advice. – Jason Lee Mar 27 '19 at 08:57
0

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

Bee
  • 12,251
  • 11
  • 46
  • 73
0

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>
simar
  • 1,782
  • 3
  • 16
  • 33