Microcks Metadata Construction – SCRIPT Dispatcher
Purpose
Guide for building Microcks APIMetadata files using the SCRIPT dispatcher for advanced dispatching logic.
Workflow
- Identify the target operation (method + path) from the OpenAPI contract.
- For each operation, define the dispatcher as
SCRIPT. - Write a Groovy script in
dispatcherRulesto select the response based on request content.
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:
<HTTP method> <operation path>:
dispatcher: SCRIPT
dispatcherRules: |
import java.util.regex.*;
def jsonSlurper = new groovy.json.JsonSlurper();
def requestContent = mockRequest.getRequestContent();
def req = jsonSlurper.parseText(requestContent);
// Custom logic here
if (req.city == "Dunkirk") {
return "BEST_CITY"
}
else if (req.city == "Paris") {
return "GOOD_CITY"
}
return "DEFAULT_CITY"
Best Practices
- Keep scripts simple and readable.
- Always validate Groovy syntax and Microcks compatibility.
- Use meaningful return values matching example names in APIExamples.