Validating swagger/openapi json using openapi-validations GitHub Action
A few months back I had a requirement of checking the APIs for the product I'm working on for breaking changes (Backward Compatibility Check) during CI/CD. As part of that research, I created an action called openapi-validations, which is essentially a wrapper on top of the following 2 prominent NPM packages:
- swagger-parser - Swagger 2.0 and OpenAPI 3.0 parser/validator
- openapi-diff - A CLI tool to identify differences between Swagger or OpenAPI specifications
While building openapi-validations action I had only 1 pre-requisite that it should support both Swagger2 and OpenAPI3 (next increment of swagger2).
This article essentially is a how-to-guide to use this action in your CI/CD effectively. So let's begin.
What openapi-validations action can do?
There are 2 primary objectives for this action.
1) Validate provided 2 swagger jsons for validation errors (always performed)
2) Verify any breaking changes between the 2 swagger specifications (can be skipped)
Additionally, this action can be configured to have ablocking_decision
, which can be configured to either mark the whole run as failed or not
Creating a Simple Test workflow based on openapi-validations
I've created this simple action workflow to demonstrate how to use it in your workflows.
The pre-requisites for this workflow are:
1) source.json file, (Source File) which is the one that one would want to test for breaking changes
2) A destination.json file (Benchmark file) which is the one that one would want to compare against for breaking changes
Depending upon whether you set blocking_decision
to 'strict' your workflow will list out all the breaking changes and will validate the swagger schema as per either swagger2 or OpenAPI3 specification
What are considered breaking changes by this workflow?
- Breaking Changes:
- Response Code changed
- Response ContentType Changed
- Response Field Type changed (for ex array to String etc)
- Endpoint path removed
- Request Method changed
- Non-Breaking Changes:
- ParamType changed (Path, Query, etc)
- RequestBody ContentType Changed
- RequestBody fieldType Changed
- RequestBody field required status changed etc
Sample results:
You can select the relevant fields in the action run workflow option:
Swagger Validation Error with blocking_decision
'strict':
Breaking changes with blocking_decision
'strict':
NOTE: Currently this action does not have support for yaml swagger schema.