How to use swagger2openapi - 10 common examples

To help you get started, we’ve selected a few swagger2openapi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mrin9 / RapiPdf / src / spec-parser.js View on Github external
export default async function ProcessSpec(specUrl, sortTags) {
  let jsonParsedSpec; let convertedSpec; let
    resolvedRefSpec;
  const convertOptions = { patch: true, warnOnly: true };
  const resolveOptions = { resolveCirculars: false };
  try {
    if (typeof specUrl === 'string') {
      convertedSpec = await converter.convertUrl(specUrl, convertOptions);
    } else {
      convertedSpec = await converter.convertObj(specUrl, convertOptions);
    }
    resolvedRefSpec = await JsonRefs.resolveRefs(convertedSpec.openapi, resolveOptions);
    jsonParsedSpec = resolvedRefSpec.resolved;
  } catch (err) {
    console.info('%c There was an issue while parsing the spec %o ', 'color:orangered', err); // eslint-disable-line no-console
  }

  const openApiSpec = jsonParsedSpec;
  const methods = ['get', 'put', 'post', 'delete', 'patch', 'options', 'head'];
  const tags = [];
  let totalPathCount = 0;
  // For each path find the tag and push it into the corrosponding tag
  for (const path in openApiSpec.paths) {
    const commonParams = openApiSpec.paths[path].parameters;
    const commonPathProp = {
      summary: openApiSpec.paths[path].summary,
github mrin9 / RapiDoc / src / utils / parse-utils.js View on Github external
url;

  const convertOptions = { patch: true, warnOnly: true };
  try {
    // JsonRefs cant load yaml files, so first use converter
    if (typeof specUrl === 'string') {
      // resolvedRefSpec = await JsonRefs.resolveRefsAt(specUrl, resolveOptions);
      convertedSpec = await converter.convertUrl(specUrl, convertOptions);
      specLocation = convertedSpec.source.trim();
      if (specLocation.startsWith('/')) {
        url = new URL(`.${specLocation}`, window.location.href);
        specLocation = url.pathname;
      }
    } else {
      // resolvedRefSpec = await JsonRefs.resolveRefs(specUrl, resolveOptions);
      convertedSpec = await converter.convertObj(specUrl, convertOptions);
      url = new URL(window.location.href);
      specLocation = url.pathname;
    }
    // convertedSpec = await converter.convertObj(resolvedRefSpec.resolved, convertOptions);
    resolveOptions = {
      resolveCirculars: false,
      location: specLocation, // location is important to specify to resolve relative external file references when using JsonRefs.resolveRefs() which takes an JSON object
    };
    resolvedRefSpec = await JsonRefs.resolveRefs(convertedSpec.openapi, resolveOptions);
    // jsonParsedSpec = convertedSpec.openapi;
    jsonParsedSpec = resolvedRefSpec.resolved;
  } catch (err) {
    console.info('%c There was an issue while parsing the spec %o ', 'color:orangered', err); // eslint-disable-line no-console
  }

  const openApiSpec = jsonParsedSpec;
github mrin9 / RapiDoc / src / utils / spec-parser.js View on Github external
let url;

  const convertOptions = { patch: true, warnOnly: true };
  try {
    // JsonRefs cant load yaml files, so first use converter
    if (typeof specUrl === 'string') {
      // resolvedRefSpec = await JsonRefs.resolveRefsAt(specUrl, resolveOptions);
      convertedSpec = await converter.convertUrl(specUrl, convertOptions);
      specLocation = convertedSpec.source.trim();
      if (specLocation.startsWith('/')) {
        url = new URL(`.${specLocation}`, window.location.href);
        specLocation = url.pathname;
      }
    } else {
      // resolvedRefSpec = await JsonRefs.resolveRefs(specUrl, resolveOptions);
      convertedSpec = await converter.convertObj(specUrl, convertOptions);
      url = new URL(window.location.href);
      specLocation = url.pathname;
    }
    // convertedSpec = await converter.convertObj(resolvedRefSpec.resolved, convertOptions);
    resolveOptions = {
      resolveCirculars: false,
      location: specLocation, // location is important to specify to resolve relative external file references when using JsonRefs.resolveRefs() which takes an JSON object
    };
    resolvedRefSpec = await JsonRefs.resolveRefs(convertedSpec.openapi, resolveOptions);
    // jsonParsedSpec = convertedSpec.openapi;
    jsonParsedSpec = resolvedRefSpec.resolved;
  } catch (err) {
    console.info('%c There was an issue while parsing the spec %o ', 'color:orangered', err); // eslint-disable-line no-console
  }

  // const pathGroups = groupByPaths(jsonParsedSpec);
github mrin9 / RapiPdf / src / spec-parser.js View on Github external
export default async function ProcessSpec(specUrl, sortTags) {
  let jsonParsedSpec; let convertedSpec; let
    resolvedRefSpec;
  const convertOptions = { patch: true, warnOnly: true };
  const resolveOptions = { resolveCirculars: false };
  try {
    if (typeof specUrl === 'string') {
      convertedSpec = await converter.convertUrl(specUrl, convertOptions);
    } else {
      convertedSpec = await converter.convertObj(specUrl, convertOptions);
    }
    resolvedRefSpec = await JsonRefs.resolveRefs(convertedSpec.openapi, resolveOptions);
    jsonParsedSpec = resolvedRefSpec.resolved;
  } catch (err) {
    console.info('%c There was an issue while parsing the spec %o ', 'color:orangered', err); // eslint-disable-line no-console
  }

  const openApiSpec = jsonParsedSpec;
  const methods = ['get', 'put', 'post', 'delete', 'patch', 'options', 'head'];
  const tags = [];
  let totalPathCount = 0;
  // For each path find the tag and push it into the corrosponding tag
  for (const path in openApiSpec.paths) {
    const commonParams = openApiSpec.paths[path].parameters;
github strongloop / loopback-next / packages / cli / generators / openapi / spec-loader.js View on Github external
async function loadSpec(specUrlStr, {log, validate} = {}) {
  if (typeof log === 'function') {
    log(chalk.blue('Loading ' + specUrlStr + '...'));
  }
  const parser = new SwaggerParser();
  let spec = await parser.parse(specUrlStr);
  if (spec.swagger === '2.0') {
    debugJson('Swagger spec loaded: ', spec);
    spec = (await swagger2openapi.convertObj(spec, {patch: true})).openapi;
    debugJson('OpenAPI spec converted from Swagger: ', spec);
  } else if (spec.openapi) {
    debugJson('OpenAPI spec loaded: ', spec);
  }

  spec = _.cloneDeepWith(spec, o => {
    if (o.$ref) {
      o['x-$ref'] = o.$ref;
    }
  });

  // Validate and deference the spec
  if (validate) {
    spec = await parser.validate(spec, {
      dereference: {
        circular: true, // Allow circular $refs
github strongloop / loopback-next / packages / booter-lb3app / src / lb3app.booter.ts View on Github external
private async buildOpenApiSpec(lb3App: Lb3Application) {
    const swaggerSpec = generateSwaggerSpec(lb3App, {
      generateOperationScopedModels: true,
    });

    // remove any properties that have values that are functions before
    // converting, as `convertObj` can't handle function values
    const fixedSwaggerSpec = JSON.parse(JSON.stringify(swaggerSpec));

    const result = await swagger2openapi.convertObj(fixedSwaggerSpec, {
      // swagger2openapi options
    });

    let spec = result.openapi as OpenApiSpec;
    if (typeof this.options.specTransformer === 'function') {
      spec = this.options.specTransformer(spec);
    }
    return spec;
  }
github Mermade / openapi-webconverter / api.js View on Github external
}).then(function(body) {
            var obj = getObj(body,payload);
            var globalOptions = options = {};
            options.origin = true;
            options.source = req.query.url;
            options.patch = true;
            options.resolve = true;
            try {
                converter.convert(obj,options,async function(err,options){
                    if (err) {
                        result.error = err.message;
                        result.options = sanitise(globalOptions);
                    }
                    else result = options.openapi;
                    if (req.query.validate && options.openapi) {
                        status.validations++;
                        try {
                            result = {};
                            result.status = await validator.validateSync(options.openapi,options);
                        }
                        catch (ex) {
                            result.status = false; // reset
                            if (options.context) {
                                result.context = options.context.pop();
                            }
github Mermade / openapi-webconverter / api.js View on Github external
var body = (req.body ? req.body.source : '')||(req.file ? req.file.buffer.toString() : '');
    var validate = (req.body && req.body.validate); // on or undefined

    var payload = parseRequest(req);

    if (req.headers['content-type'].startsWith('application/x-www-form-urlencoded')) {
        result.warning = `Your client sent the wrong Content-Type header (${req.headers['content-type']}). Try pasting your document`;
        payload.status = 400;
        if (!req.body.source && !req.file) payload.status = 200; // Dredd
    }
    var obj = getObj(body,payload);
    var options = {};
    options.patch = true;
    options.resolve = false;
    try {
        converter.convert(obj,options,async function(err,options){
            if (err) {
                result.message = err.message||'no message';
                result.options = sanitise(result.options);
            }
            else {
                result = options.openapi;
                if (options.openapi && options.openapi.openapi && options.openapi.openapi.startsWith('3.')) payload.status = 200;
            }
            if (validate && !err) {
                status.validations++;
                try {
                    result = {};
                    result.status = await validator.validateSync(options.openapi,options);
                    if (result.status === true) payload.status = 200;
                }
                catch (ex) {
github mrin9 / RapiDoc / src / utils / parse-utils.js View on Github external
export default async function ProcessSpec(specUrl) {
  let jsonParsedSpec;
  let convertedSpec;
  let resolvedRefSpec;
  let resolveOptions;
  let specLocation = ''; let
    url;

  const convertOptions = { patch: true, warnOnly: true };
  try {
    // JsonRefs cant load yaml files, so first use converter
    if (typeof specUrl === 'string') {
      // resolvedRefSpec = await JsonRefs.resolveRefsAt(specUrl, resolveOptions);
      convertedSpec = await converter.convertUrl(specUrl, convertOptions);
      specLocation = convertedSpec.source.trim();
      if (specLocation.startsWith('/')) {
        url = new URL(`.${specLocation}`, window.location.href);
        specLocation = url.pathname;
      }
    } else {
      // resolvedRefSpec = await JsonRefs.resolveRefs(specUrl, resolveOptions);
      convertedSpec = await converter.convertObj(specUrl, convertOptions);
      url = new URL(window.location.href);
      specLocation = url.pathname;
    }
    // convertedSpec = await converter.convertObj(resolvedRefSpec.resolved, convertOptions);
    resolveOptions = {
      resolveCirculars: false,
      location: specLocation, // location is important to specify to resolve relative external file references when using JsonRefs.resolveRefs() which takes an JSON object
    };
github mrin9 / RapiDoc / src / utils / spec-parser.js View on Github external
export default async function ProcessSpec(specUrl, sortTags = false, attrApiKey = '', attrApiKeyLocation = '', attrApiKeyValue = '') {
  let jsonParsedSpec;
  let convertedSpec;
  let resolvedRefSpec;
  let resolveOptions;
  let specLocation = '';
  let url;

  const convertOptions = { patch: true, warnOnly: true };
  try {
    // JsonRefs cant load yaml files, so first use converter
    if (typeof specUrl === 'string') {
      // resolvedRefSpec = await JsonRefs.resolveRefsAt(specUrl, resolveOptions);
      convertedSpec = await converter.convertUrl(specUrl, convertOptions);
      specLocation = convertedSpec.source.trim();
      if (specLocation.startsWith('/')) {
        url = new URL(`.${specLocation}`, window.location.href);
        specLocation = url.pathname;
      }
    } else {
      // resolvedRefSpec = await JsonRefs.resolveRefs(specUrl, resolveOptions);
      convertedSpec = await converter.convertObj(specUrl, convertOptions);
      url = new URL(window.location.href);
      specLocation = url.pathname;
    }
    // convertedSpec = await converter.convertObj(resolvedRefSpec.resolved, convertOptions);
    resolveOptions = {
      resolveCirculars: false,
      location: specLocation, // location is important to specify to resolve relative external file references when using JsonRefs.resolveRefs() which takes an JSON object
    };