Skip to content

Instantly share code, notes, and snippets.

@seriousme
Last active August 19, 2020 18:16
Show Gist options
  • Save seriousme/1c262ae3a5f46c413a29e662cd2fa78b to your computer and use it in GitHub Desktop.
Save seriousme/1c262ae3a5f46c413a29e662cd2fa78b to your computer and use it in GitHub Desktop.
Transforming an OpenAPi v3 spec
// 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