This repository demonstrates the power of overlays (and the support by Specmatic) - a feature that allows you to extend and modify OpenAPI specifications without changing the original contract. This is particularly useful when you need to:
- Add common headers across multiple endpoints
- Extend security requirements
- Add new parameters for monitoring or tracing
- Modify specifications for different environments
The demo consists of three key files:
employees.yaml
- The base OpenAPI specification for an employee serviceemployees_overlay.yaml
- An overlay that adds required headers for API gateway integrationspecmatic.yaml
- Configuration file for Specmatic
The overlay adds three important headers to the POST /employees endpoint:
- X-Correlation-ID: For request tracking
- X-Gateway-Token: For API gateway authentication
- X-Request-ID: For request tracing
For a complete understanding of Specmatic overlays, please refer to the official documentation.
You can run this demo using Docker, Specmatic Jar or Specmatic Node package. The demo involves two steps:
- Starting the stub server
- Running the tests to see overlay validation in action
java -jar specmatic.jar stub
java -jar specmatic.jar test --port=9000
docker run -p="9000:9000" -v="./specmatic.yaml:/usr/src/app/specmatic.yaml" -v="./employees.yaml:/usr/src/app/employees.yaml" -v="./employees_overlay.yaml:/usr/src/app/employees_overlay.yaml" znsio/specmatic stub
docker run -v="./specmatic.yaml:/usr/src/app/specmatic.yaml" -v="./employees.yaml:/usr/src/app/employees.yaml" -v="./employees_overlay.yaml:/usr/src/app/employees_overlay.yaml" znsio/specmatic test --port=9000 --host=host.docker.internal
npm install -g specmatic
npx specmatic stub
npx specmatic test
When you run the tests, you'll notice that:
- Specmatic start the
stub
server starts and loads both the base contract and overlay - Specmatic loads both the base ccontract and overlay for
test
also, and then requests with all required headers are used for testing. - Test requests without the required headers (added by overlay) will fail
This demonstrates how overlays can enforce additional requirements without modifying the base contract.