Microcks Metadata Construction – JSON_BODY Dispatcher
Purpose
Guide for building Microcks APIMetadata files using the JSON_BODY dispatcher.
Workflow
- Identify the target operation (method + path) from the OpenAPI contract.
- For each operation, define the dispatcher as
JSON_BODY. - Set
dispatcherRulesto the field in the request body that will be used for dispatching (e.g.,/city).
Example
apiVersion: mocks.microcks.io/v1alpha1
kind: APIMetadata
metadata:
name: <Same name as in OpenAPI ${input:file-name}>
version: <Same contract version in OpenAPI>
operations:
POST /city/evaluate:
dispatcher: JSON_BODY
dispatcherRules: |
{
"exp": "/city", # JSON Pointer to the field in the request body
"op": "equals", # Operator to apply (equals, range, regexp, etc.)
"cases": {
"Dunkirk": "BEST_CITY", # Value to match => Example name
"Paris": "GOOD_CITY", # Another value to match => Example name
"default": "DEFAULT_CITY" # Default value if no match
}
}
Explanation
expis a JSON Pointer expression to extract the value from the request body (e.g./cityfor{ "city": "..." }).opis the operator to apply (most common:equals).casesmaps possible values to the example names defined in your APIExamples file. Thedefaultkey is used if no value matches.
Example request body:
{
"city": "Dunkirk"
}
This will select the example named BEST_CITY.
Best Practices
- Use a field that is always present and unique for dispatching.
- Keep the metadata file strictly in Microcks format (no OpenAPI header).
- Validate YAML syntax.