The switch policy in API Studio for the DataPower Nano Gateway: worked examples
Draft!!
A switch policy runs the first case whose condition is true. That is the whole rule. This post shows it working in seven ways, with the YAML from API Studio code view and the response the gateway returned.
The companion post covers the invoke policy.
How switch works
A Switch has a list of cases and an optional otherwise. Each case has a condition and an execute list of policies. The condition is a JSONata expression. The reference page says the switch runs “the first assembly that matched the given expressions”. Cases are checked top to bottom and the first true one wins. cases needs at least one entry.
The conditions use the JSONata functions the Nano Gateway supports. These are the ones used below:
| Function | Returns |
|---|---|
$queryParameter(message_name, query_name) |
A query parameter from a message |
$header(message_name, header_name) |
A header from a message |
$pathParameter(param_name) |
A path parameter from the request |
$number, $length, $uppercase, $contains, $exists |
Number, string and boolean helpers |
Comparison operators =, !=, <, >, <=, >= and boolean and, or are supported.
The test project
One API Studio project, one API called nano-switch, one operation per example. Each branch runs a set policy that writes a small JSON document to the response, so the body says which case matched.
kind: Set
spec:
messageBody:
messageName: response
value: '{"matched":"alpha","via":"switch case 1 of 3","condition":"case = alpha"}'
The policy sequence extracts client credentials, authenticates, authorises, sets Content-Type: application/json on the response, then hands over to an OperationSwitch:

(Note: the canvas view above is cut off at the bottom due to the vertical size of the full flow.)
The OperationSwitch chooses by path and verb. Switch chooses by anything you can write in JSONata. I use OperationSwitch at the top and Switch inside each operation:
kind: OperationSwitch
spec:
cases:
- conditions:
- verbPath:
path: /basic
verb: GET
execute:
- $ref: nanogw-policy-samples:nano-switch-basic:1.0.0
- conditions:
- verbPath:
path: /resource/{id}
verb: GET
execute:
- $ref: nanogw-policy-samples:nano-switch-resource:1.0.0
# /first-match, /numeric, /strings, /headers and /nested follow the same pattern
OperationSwitch conditions can be verbPath, path, verb or operationId.
Calls look like this:
curl -s \
-H "X-IBM-Client-Id: <client-id>" \
-H "X-IBM-Client-Secret: <client-secret>" \
"https://<product>-<catalog>-<org>.nanogw.<ingress-domain>/1.0.0/nano-switch/basic?case=alpha"
Without valid credentials the call returns 401 Unauthorized. I tested no credentials, a wrong ID, a wrong secret, an ID with no secret, a secret with no ID and empty headers. All returned 401.
Example 1: three cases and an otherwise
kind: Switch
spec:
cases:
- condition: "$queryParameter('request','case') = 'alpha'"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-case-alpha:1.0.0
- condition: "$queryParameter('request','case') = 'beta'"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-case-beta:1.0.0
- condition: "$queryParameter('request','case') = 'gamma'"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-case-gamma:1.0.0
otherwise:
- $ref: nanogw-policy-samples:nano-switch-set-case-default:1.0.0
$queryParameter('request','case') reads the case query parameter from the inbound request message.
| Request | Response |
|---|---|
?case=alpha |
{"matched":"alpha","via":"switch case 1 of 3","condition":"case = alpha"} |
?case=beta |
{"matched":"beta","via":"switch case 2 of 3","condition":"case = beta"} |
?case=gamma |
{"matched":"gamma","via":"switch case 3 of 3","condition":"case = gamma"} |
?case=delta |
{"matched":"none","via":"switch otherwise (no case condition was true)"} |
no case parameter |
{"matched":"none","via":"switch otherwise (no case condition was true)"} |
All five returned 200. A missing parameter does not cause an error. It falls through to otherwise.
Example 2: the first match wins
These two cases overlap on apple:
kind: Switch
spec:
cases:
- condition: "($queryParameter('request','value') = 'apple') or ($queryParameter('request','value') = 'banana')"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-fruit-basket:1.0.0
- condition: "$queryParameter('request','value') = 'apple'"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-unreachable-apple:1.0.0
otherwise:
- $ref: nanogw-policy-samples:nano-switch-set-no-match:1.0.0
GET /first-match?value=apple
{"matched":"fruit-basket","via":"case 1 of 2","condition":"value = apple or value = banana (first match wins)"}
banana returns the same response. cherry returns {"matched":"no-match","via":"otherwise"}. The second case never runs for apple, and nothing ever returns its response. Order your cases from most specific to least specific.
Example 3: numbers
kind: Switch
spec:
cases:
- condition: "$number($queryParameter('request','value')) > 100"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-greater-100:1.0.0
- condition: "$number($queryParameter('request','value')) >= 50"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-between-50-100:1.0.0
otherwise:
- $ref: nanogw-policy-samples:nano-switch-set-less-50:1.0.0
| Request | Response |
|---|---|
?value=150 |
{"matched":"greater-than-100","condition":"$number(value) > 100"} |
?value=75 |
{"matched":"50-to-100","condition":"$number(value) >= 50 and $number(value) <= 100"} |
?value=10 |
{"matched":"less-than-50","condition":"otherwise"} |
The second case only tests >= 50. The <= 100 in the response text is a label, not part of the condition. It works because case 1 already took everything above 100.
Example 4: strings
kind: Switch
spec:
cases:
- condition: "$contains($queryParameter('request','value'), 'urgent')"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-escalated:1.0.0
- condition: "$length($queryParameter('request','value')) > 20"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-too-long:1.0.0
- condition: "($length($queryParameter('request','value')) > 0) and ($uppercase($queryParameter('request','value')) = $queryParameter('request','value'))"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-upper-case:1.0.0
otherwise:
- $ref: nanogw-policy-samples:nano-switch-set-default-string:1.0.0
| Request | Response |
|---|---|
?value=this is urgent |
{"matched":"escalated","condition":"$contains(value, urgent)"} |
?value=a very long string that exceeds twenty chars |
{"matched":"too-long","condition":"$length(value) > 20 and not containing urgent"} |
?value=SCREAMING |
{"matched":"upper-case","condition":"length > 0 and $uppercase(value) = value"} |
?value=hello |
{"matched":"default-string","via":"otherwise"} |
The upper-case test compares the value with its own $uppercase(). The $length() > 0 guard stops an empty value counting as upper case.
Case order matters again. A long string containing urgent hits case 1 and is reported as escalated, not too-long.
Example 5: request headers
kind: Switch
spec:
cases:
- condition: "$header('request','x-priority') = 'high'"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-priority-high:1.0.0
- condition: "$exists($header('request','x-priority'))"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-priority-low:1.0.0
otherwise:
- $ref: nanogw-policy-samples:nano-switch-set-no-priority:1.0.0
| Request | Response |
|---|---|
-H 'x-priority: high' |
{"matched":"priority-high","condition":"request header x-priority = high"} |
-H 'x-priority: low' |
{"matched":"priority-low","condition":"request header x-priority exists but is not high"} |
| no header | {"matched":"no-priority","via":"otherwise"} |
$exists() in case 2 catches any value that case 1 did not take.
Example 6: path parameters
The OpenAPI path is /resource/{id}. $pathParameter('id') reads the id segment:
kind: Switch
spec:
cases:
- condition: "$pathParameter('id') = 'gold'"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-gold:1.0.0
- condition: "$pathParameter('id') = 'silver'"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-silver:1.0.0
otherwise:
- $ref: nanogw-policy-samples:nano-switch-set-standard:1.0.0
| Request | Response |
|---|---|
/resource/gold |
{"matched":"gold","condition":"$pathParameter(id) = gold"} |
/resource/silver |
{"matched":"silver","condition":"$pathParameter(id) = silver"} |
/resource/bronze |
{"matched":"standard","via":"otherwise"} |
The reference page shows the same function with a different parameter name: $pathParameter("courierScac") = 'INPT'.
Example 7: a switch inside a switch
The execute list of a case holds policy references, and a switch is a policy. The outer switch routes on primary. When primary=inner, it runs a second switch that routes on secondary.
kind: Switch # outer
spec:
cases:
- condition: "$queryParameter('request','primary') = 'inner'"
execute:
- $ref: nanogw-policy-samples:nano-switch-nested-inner:1.0.0
- condition: "$queryParameter('request','primary') = 'outer'"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-outer-only:1.0.0
otherwise:
- $ref: nanogw-policy-samples:nano-switch-set-neither:1.0.0
kind: Switch # inner
spec:
cases:
- condition: "$queryParameter('request','secondary') = 'x'"
execute:
- $ref: nanogw-policy-samples:nano-switch-set-inner-x:1.0.0
otherwise:
- $ref: nanogw-policy-samples:nano-switch-set-inner-other:1.0.0
| Request | Response |
|---|---|
?primary=inner&secondary=x |
{"matched":"inner-x","via":"nested switch case 1","condition":"secondary = x"} |
?primary=inner&secondary=y |
{"matched":"inner-other","via":"nested switch otherwise"} |
?primary=outer |
{"matched":"outer","via":"outer switch case 2","condition":"primary = outer"} |
?primary=zzz |
{"matched":"neither","via":"outer switch otherwise"} |
secondary is ignored unless primary=inner.
Switch, If or OperationSwitch
- Switch: several conditions, one branch runs.
- If: one condition,
executeruns when it is true. Use it when there is nothing to do otherwise. - OperationSwitch: branch on the path, verb or operation ID of the API call. No JSONata needed.
The Studio documentation lists all three under logic constructs, along with Return, Throw and Try.
Summary
Put the most specific case first. Always add an otherwise. Keep each case to one execute list of policy references, and nest a switch when a decision depends on an earlier one.
Limitations and Notes
- Tested on a DataPower Nano Gateway with the API Connect 12.1 documentation set. Check the version you run.
- The reference page marks
otherwiseas optional. I did not test a switch without one, so I cannot say what the caller gets when no case matches. - I wrapped the query parameter in
$number()for the numeric example because it arrives in the URL. I did not test without it, or with non-numeric input. - The Studio code view sample for
Switchuses$header('request', 'Content-Type') in [...]. Theinoperator is not in the list of supported Nano Gateway JSONata operators, so I did not use it. - The JSONata function list says several operators have partial support, for example the numeric ones. Check the supported functions page before writing anything complicated.
