Last active
August 19, 2020 18:16
-
-
Save seriousme/1c262ae3a5f46c413a29e662cd2fa78b to your computer and use it in GitHub Desktop.
Transforming an OpenAPi v3 spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// small transformation utility to modify an openApi spec | |
// tested against: https://github.com/ringcentral/ringcentral-api-specifications/blob/master/ringcentral.spec.2019110220191017-1140.openapi3.yaml | |
const yaml = require('js-yaml'); | |
const fs = require('fs'); | |
const condition = 'x-auth-required'; | |
const filename = 'ringcentral.spec.openapi3.yaml'; | |
// Transformation function | |
function transform(spec) { | |
for (const path in spec.paths) { | |
const pathItem = spec.paths[path]; | |
for (const method in pathItem) { | |
const methodItem = pathItem[method]; | |
if (methodItem[condition] === false) { | |
methodItem.security = [{}] | |
console.log(path, method, methodItem); | |
} | |
} | |
} | |
} | |
// Get document, or throw exception on error | |
const rawSpec = yaml.safeLoad(fs.readFileSync(filename, 'utf8')); | |
const transformedSpec = transform(rawSpec); | |
// and now use the spec in fastify openapi glue or write it | |
// to a JSON or YAML file and use that | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment