Sanitizes unused definitions
Project description
openapi_spec_sanitizer
A Sanitizer for OpenAPI Yaml spec files
Description
Offers a CLI, or simple API, to detect, report, and, optionally, fix unused components in OpenAPI specifications
- Detects unused, or undefined, components in Swagger (2.0) and OpenAPI (3.*) (soz: yaml-only) specifications
- It can sanitize discovered unused components, either by:
- deleting the component
- or adding a new tag to the component
- Sanitised yaml is stored to file
- OpenAPI/Swagger Spec files can be loaded from URI or file
- It always tries not to overwrite existing files(yay!)
- It detects (unsupported so far) remote references and politely gives up
This package was written with a single purpose in mind, and makes no claims other than proving quite useful for that particular purpose.
Hopefully it will find a more general use.
TODO
It doesn't do many things, but of note it might be useful to(most-likely first):
- Add support for JSON-format Specification
- Add support for remote references, which depends on:
- Add support for multiple files
- Add support for caching remote (url) specifications to file
Author
David Turland, david@turland.org
Install
From PyPi:
pip install openapi-spec-sanitizer
From source:
python3 setup.py install
# or
python3 setup.py install --user
Usage
usage: openapi_spec_sanitizer [-h] [-c CACHEDIR] [-s] [-t TAG | -r]
[-o OUTPUT] [-l] [-v] [-g] [--version]
filename
Sanitize OpenAPI.
options:
-h, --help show this help message and exit
-o OUTPUT, --output OUTPUT
output file name for sanitized YAML
-l, --lax Yaml syntax warnings are tolerable
-v, --verbose
-g, --debug
--version show the version number and exit
Yaml Loading Options:
filename OpenAPI specification: file path or url (yaml-only)
Sanitizing Options:
-s, --sanitize Attempt to sanitize spec file (default False)
-t TAG, --tag TAG sanitize mode is to tag component
-r, --remove Sanitize mode is to remove component
Examples
Testing for Unused and Undefined Component detection
openapi_spec_sanitizer tests/simple.yaml -l
------------------------ Analyzer Report ----------------
Undefined components
'/components/requestBodies/requestBodyMissingRequired'
'/components/schemas/schemaPlainMissingUnused'
Uunused components
path: /components/requestBodies/requestBodyAUnused
Type: Component, path: /components/requestBodies/requestBodyAUnused, line: 15, is component? True, is declared? True, is required? False
----------------------- ~Analyzer Report ----------------
Sanitizing Unused components
Here we have an OpenAPI spec with an unused component,/components/requestBodies/requestBodyAUnused
openapi: 3.0.0
paths:
/wibble:
post:
summary: wobble
requestBody:
application/json:
schema:
type: "string"
responses:
'201':
$ref: '#/components/responses/responseA'
components:
parameters:
requestBodies:
requestBodyAUnused:
description: requestBodyAUnused description
required: true
content:
application/json:
schema:
type: "string"
responses:
responseA:
description: responseA
headers:
Floob:
schema:
type: "string"
Running openapi_spec_sanitizer
with -s
to sanitize
NOTE the default mode is to tag unused components with unused
openapi_spec_sanitizer.exe tests/simple_unused.yaml -s
...
Main: dumping sanitized yaml to tests/simple_unused.san.yaml
------------------------ Analyzer Report ----------------
Uunused components
path: /components/requestBodies/requestBodyAUnused
Type: Component, path: /components/requestBodies/requestBodyAUnused, line: 17, is component? True, is declared? True, is required? False
----------------------- ~Analyzer Report ----------------
Yields this sanitized yaml:
note the unused component tagged with unused
openapi: 3.0.0
paths:
/wibble:
post:
summary: wobble
requestBody:
application/json:
schema:
type: string
responses:
'201':
$ref: '#/components/responses/responseA'
components:
parameters: null
requestBodies:
requestBodyAUnused:
description: requestBodyAUnused description
required: true
content:
application/json:
schema:
type: string
unused: true
responses:
responseA:
description: responseA
headers:
Floob:
schema:
type: string
Tests
Basic Testing
python3 setup.py test
OpenBanking Specification Tests
Tests some of the OpenAPI spec files from the OpenBanking Official Open Banking UK API Standards
And yes (as of 14/2/23) there are unused components...
NOTE:
- These tests are not run as part of python setuptools
- Remote spec files are cached once and used locally
python3 setup.py test -s tests.openbanking
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for openapi-spec-sanitizer-0.1.10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | afc5423907d2f87aec1c541888b5768f2c2725632c1f0caadb321c3201daeeba |
|
MD5 | fa82b495d50eecdaaa82d19d0c37b82b |
|
BLAKE2b-256 | ab8ccfc27dfdb6990b5e90b3747807e3bf7f45ea4d9171c1ad72eb0b5397e00a |
Hashes for openapi_spec_sanitizer-0.1.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e697abe8638c5361cd3a213a0dab0cb03b9ade223b49de7ed45af488f9f6931 |
|
MD5 | ed45efade6eda4e7a3d5f51557768eea |
|
BLAKE2b-256 | 935956c200e46b334bcbb034145c2cd8872216ea80a346041c5586e7285a1538 |