Package: openapi-spec-sanitizer
openapi-spec-sanitizerA Sanitizer forOpenAPIYAML, or JSON, spec filesDescriptionOffers a CLI, or simple API, to detect, report, and, optionally, fix unused components in OpenAPI specificationsDetects unused, or undefined, components in Swagger (2.0) and OpenAPI (3.*) (YAML or json) specificationsIt cansanitizediscovered unused components, either by:deleting the componentor adding a new tag to the componentSanitised YAML, or json, is stored to fileOpenAPI/Swagger Spec files can be loaded from URI or fileIt always tries not to overwrite existing files(yay!)It detects (unsupported so far) remote references and politely gives upThis package was written with asingle purposein mind, and makes no claims other than provingquiteuseful for that particular purpose.Hopefully it will find a more general use.TODOIt doesn't do many things, but of note it might be useful to(most-likely first):Add support for JSON-format SpecificationAdd support for remote references, which depends on:Add support for multiple filesAdd support for caching remote (url) specifications to fileAuthorDavid Turland,david@turland.orgInstallFrom PyPi:pipinstallopenapi-spec-sanitizerFrom source:python3-mpipinstall# orpython3-mpipinstall--userUsageusage:openapi_spec_sanitizer[-h][-cCACHEDIR][-s][-tTAG|-r][-oOUTPUT][-l][-v][-g][--version]filename

SanitizeOpenAPI.

options:-h,--helpshowthishelpmessageandexit-oOUTPUT,--outputOUTPUToutputfilenameforsanitizedYAML-l,--laxYAML,orJSON,syntaxwarningsaretolerable-v,--verbose-g,--debug--versionshowtheversionnumberandexitYAML,orJSON,LoadingOptions:filenameOpenAPIspecification:filepathorurl(YAML,orJSON)SanitizingOptions:-s,--sanitizeAttempttosanitizespecfile(defaultFalse)-tTAG,--tagTAGsanitizemodeistotagcomponent-r,--removeSanitizemodeistoremovecomponentExamplesTesting for Unused and Undefined Component detectionopenapi_spec_sanitizertests/simple.yaml-l------------------------AnalyzerReport----------------
Undefinedcomponents'/components/requestBodies/requestBodyMissingRequired''/components/schemas/schemaPlainMissingUnused'Uunusedcomponentspath:/components/requestBodies/requestBodyAUnusedType:Component,path:/components/requestBodies/requestBodyAUnused,line:15,iscomponent?True,isdeclared?True,isrequired?False
-----------------------~AnalyzerReport----------------Sanitizing Unused componentsHere we have an OpenAPI spec with an unused component,/components/requestBodies/requestBodyAUnusedopenapi:3.0.0paths:/wibble:post:summary:wobblerequestBody:application/json:schema:type:"string"responses:'201':$ref:'#/components/responses/responseA'components:parameters:requestBodies:requestBodyAUnused:description:requestBodyAUnused descriptionrequired:truecontent:application/json:schema:type:"string"responses:responseA:description:responseAheaders:Floob:schema:type:"string"Runningopenapi_spec_sanitizerwith-sto sanitizeNOTEthe default mode is to tag unused components withunusedopenapi_spec_sanitizer.exetests/simple_unused.yaml-s...Main:dumpingsanitizedYAMLtotests/simple_unused.san.yaml------------------------AnalyzerReport----------------Uunusedcomponentspath:/components/requestBodies/requestBodyAUnusedType:Component,path:/components/requestBodies/requestBodyAUnused,line:17,iscomponent?True,isdeclared?True,isrequired?False-----------------------~AnalyzerReport----------------Yields this sanitized YAML:notethe unused component tagged withunusedopenapi:3.0.0paths:/wibble:post:summary:wobblerequestBody:application/json:schema:type:stringresponses:'201':$ref:'#/components/responses/responseA'components:parameters:nullrequestBodies:requestBodyAUnused:description:requestBodyAUnused descriptionrequired:truecontent:application/json:schema:type:stringunused:trueresponses:responseA:description:responseAheaders:Floob:schema:type:stringTestsBasic TestingpytestOpenBanking Specification TestsTests some of the OpenAPI spec files from theOpenBankingOfficial Open Banking UK API StandardsAnd yes (as of 14/2/23) there are unused components...NOTE:These tests are not run as part of python setuptoolsRemote spec files are cached once and used locallypytesttests/openbankingAttributionsOpenAPI Initiative OpenAPI-Specification- A snapshot of one of their sample OpenAPI spec files,api-with-examples.yaml, is stored in this repo, and
is used in one of the unit tests (It passes BTW :-) )