If a client fails against a spec-driven mock, it usually means there is a mismatch between the client's request/response and the provider's expected request/response format. This lab will help you understand how to identify such mismatches and use Specmatic's data adapter to temporarily adapt the provider service's interface to match the consumer's expected interface. This allows you to continue development and testing without waiting for the provider to fix their specification, while still ensuring that your expectations are valid against the specification.
10-15 minutes.
- Docker is installed and running.
- You are in
labs/data-adapters.
camel-case-service(Specmatic mock): serves the contract from.specmatic/repos/labs-contracts/openapi/data-adapters/camelCase.yamlonhttp://127.0.0.1:9090uiservice (Nginx): serves a small web page onhttp://127.0.0.1:8080- Browser UI sends a PascalCase request to camel-case-service, which expects request in camelCase
.specmatic/repos/labs-contracts/openapi/data-adapters/camelCase.yaml: Provider contract used byspecmatic.yamlto start the mock service after the contracts repo is checked out.specmatic.yaml: Specmatic configuration.ui/index.html: Simple UI that sends PascalCase query/header/body fields.ui/default.conf: Nginx config that serves UI and proxies/testto the mock.hooks/pre_specmatic_request_processor.sh: Converts outgoing request keys from PascalCase to camelCase.hooks/post_specmatic_response_processor.sh: Converts incoming response keys from camelCase to PascalCase.
- Processor hooks configuration: https://docs.specmatic.io/documentation/configuration.html#hooks
- Processor hooks concept: https://docs.specmatic.io/features/hooks/processor_hooks
- Do not edit the spec in
.specmatic/repos/labs-contracts/openapi/data-adapters/camelCase.yaml. - Do not change
docker-compose.yaml. - Do not touch the
ui/index.html. - Fix the mismatch only by wiring the existing hook scripts in
specmatic.yaml.
Run:
docker compose up- Open
http://127.0.0.1:8080. - Keep default values and click Submit button.
- Observe a 400 bad-request response in the result panel.
You can verify the same failure directly against the service running on 9090:
docker compose --profile test run --rm verifierHTTP/1.1 400 Bad Request
Expected result:
- The response status should be
400 Bad Request. - This confirms that, before adapters are configured, the service on
9090rejects the PascalCase request because the service expects camelCase fields.
Why it fails:
- The UI sends PascalCase fields (
RequestQuery,RequestHeader,RequestKey). - The provider service (mock in our case) expects camelCase fields (
requestQuery,requestHeader,requestKey).
This fail-first behavior is expected in this lab.
Run:
docker compose down -vAdd the following data block under dependencies in specmatic.yaml so that it sits alongside services:
dependencies:
services:
# existing service config
data:
adapters:
pre_specmatic_request_processor: ./hooks/pre_specmatic_request_processor.sh
post_specmatic_response_processor: ./hooks/post_specmatic_response_processor.shWhy both hooks are needed:
pre_specmatic_request_processor: adapts PascalCase request fields to camelCase before sending to mock.post_specmatic_response_processor: adapts camelCase mock response fields back to PascalCase before assertion.
Run:
chmod +x hooks/pre_specmatic_request_processor.sh hooks/post_specmatic_response_processor.shRun:
docker compose up- Open
http://127.0.0.1:8080. - Keep default values and click Submit button.
- Observe a 200 response in the result panel.
Run:
docker compose --profile test run --rm verifierHTTP/1.1 200 OK
Expected result:
- The response status should be
200 OK. - The response should include the adapted response fields, showing that the mock on
9090is now accepting the PascalCase request and returning a successful response through the configured adapters.
Run:
docker compose down -v- If you use PowerShell or CMD,
chmodmay not work. Use Git Bash for this step, or run:
git update-index --chmod=+x hooks/pre_specmatic_request_processor.sh hooks/post_specmatic_response_processor.sh- Ensure hook files use LF line endings (not CRLF). In Git Bash:
sed -i 's/\r$//' hooks/pre_specmatic_request_processor.sh hooks/post_specmatic_response_processor.sh- If the test still fails with the same
RequestQuery/requestQuerymismatch after adding adapters, it usually means hook scripts were not executed. Re-check execute bit and LF line endings.
Start Studio:
docker compose --profile studio up -d studio ui- Open
http://127.0.0.1:9000/_specmatic/studio, - Open
specmatic.yaml, run the suite or start the mock from theMocktab, and use port9090. If you want to inspect the contract file in Studio, open.specmatic/repos/labs-contracts/openapi/data-adapters/camelCase.yamlfrom the left panel after the repo is checked out. - Open
http://127.0.0.1:8080. - Keep default values and click Submit button.
- Observe a 200 response in the result panel.
- Go to the
Mocktab and look at the request and response payloads. You should see how the data adapters have transformed the field names in both directions.
Run:
docker compose --profile studio down -vIf you are doing this lab as part of an eLearning course, return to the eLearning site and continue with the next module.