Socket
Socket
Sign inDemoInstall

swagger2openapi

Package Overview
Dependencies
96
Maintainers
1
Versions
156
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    swagger2openapi

Convert Swagger 2.0 definitions to OpenApi 3.0 and validate


Version published
Weekly downloads
1.2M
decreased by-15.96%
Maintainers
1
Install size
5.28 MB
Created
Weekly downloads
 

Package description

What is swagger2openapi?

The swagger2openapi npm package is designed to convert Swagger 2.0 definitions into OpenAPI 3.0.x, facilitating the transition to the newer OpenAPI specification. It supports both command-line and programmatic usage, making it a versatile tool for developers working with API documentation and specifications.

What are swagger2openapi's main functionalities?

Conversion of Swagger 2.0 to OpenAPI 3.0

This feature allows users to programmatically convert Swagger 2.0 API definitions to OpenAPI 3.0 specifications. The code sample demonstrates how to load a Swagger 2.0 JSON file, convert it, and then log the resulting OpenAPI 3.0 specification.

const converter = require('swagger2openapi');
const swagger = require('./swagger.json');

converter.convertObj(swagger, {}, (err, options) => {
  if (err) {
    console.error(err);
  } else {
    console.log(options.openapi);
  }
});

Validation of converted OpenAPI 3.0 definitions

Beyond conversion, the package also offers validation of the resulting OpenAPI 3.0 definitions. This ensures that the conversion process not only translates the specification but also checks for any inconsistencies or errors in the new format.

const converter = require('swagger2openapi');
const swagger = require('./swagger.json');

converter.convertObj(swagger, {validate: true}, (err, options) => {
  if (err) {
    console.error('Validation error:', err);
  } else {
    console.log('Validation successful. OpenAPI 3.0 output:', options.openapi);
  }
});

Command-line conversion

For users preferring command-line tools, swagger2openapi provides a straightforward way to convert Swagger 2.0 files to OpenAPI 3.0 by specifying the input and output files directly in the terminal.

swagger2openapi swagger.json -o openapi.json

Other packages similar to swagger2openapi

Readme

Source

swagger2openapi

logo

OpenAPI Validation Build Tested on APIs.guru Tested on Mermade OpenAPIs Coverage Status Known Vulnerabilities Greenkeeper badge

Convert Swagger 2.0 definitions into OpenApi 3.0.x

Currently tracking v3.0.0

Usage:

swagger2openapi [options] [filename|url]
Options:
  --warnProperty    Property name to use for warning extensions
                                             [string] [default: "x-s2o-warning"]
  --version         Show version number                                [boolean]
  -c, --components  output information to unresolve a definition       [boolean]
  -d, --debug       enable debug mode, adds specification-extensions   [boolean]
  -e, --encoding    encoding for input/output files   [string] [default: "utf8"]
  -h, --help        Show help                                          [boolean]
  -i, --indent      JSON indent to use, defaults to 4 spaces            [string]
  -o, --outfile     the output file to write to                         [string]
  -p, --patch       fix up small errors in the source definition       [boolean]
  -r, --resolve     resolve external references                        [boolean]
  -u, --url         url of original spec, creates x-origin entry        [string]
  -v, --verbose     increase verbosity                                   [count]
  -w, --warnOnly    Do not throw on non-patchable errors, add warning extensions
                                                                       [boolean]
  -y, --yaml        read and write YAML, default JSON                  [boolean]

or use the APIs:

var converter = require('swagger2openapi');
var options = {};
//options.patch = true; // fix up small errors in the source definition
//options.warnOnly = true; // Do not throw on non-patchable errors
converter.convertObj(swagger, options, function(err, options){
  // options.openapi contains the converted definition
});
// also available are asynchronous convertFile, convertUrl, convertStr and convertStream functions
// if you omit the callback parameter, you will instead receive a Promise
var validator = require('swagger2openapi/validate.js');
var options = {};
validator.validate(openapi, options, function(err, options){
  // options.valid contains the result of the validation
  // options.context now contains a stack (array) of JSON-Pointer strings
});
// also available is a synchronous validateSync method which returns a boolean

See here for complete documentation of the options object.

Or use the online version which also includes its own API.

Browser Support

See initial documentation.

Features

OpenAPI 3.0.x validation

The testRunner harness can also be used as a simple validator if given one or more existing OpenAPI 3.x definitions. The validator (however it is called) uses WHATWG URL parsing if available (node 7.x and above).

Reference preservation

swagger2openapi preserves $ref JSON references in your API definition, and does not dereference every item, as with some model-based parsers.

Schema transformations

Swagger2openapi will automatically 'repair' a number of problems where non-compliant Swagger 2.0 schemas have been used. It will attempt to transform JSON schemas (used incorrectly) into OpenAPI 3.0.x Schema objects.

Specification extensions

swagger2openapi has support for a limited number of real-world specification extensions which have a direct bearing on the conversion. All other specification extensions are left untouched. swagger2openapi is swaggerplusplus-compatible.

It is expected to be able to configure the process of specification-extension modification using options or a plugin mechanism in a future release.

Tests

To run a test-suite:

node testRunner [-f {path-to-expected-failures}]... [{path-to-APIs|single-file...}]

The test harness currently expects files with a .json or .yaml extension, or a single named file, and has been tested on Node.js versions 4.x, 6.x and 8.7 (it is not recommended to run the test suite under Node.js version 7.x or Node.js versions less than 8.7.0 because of this bug) against

Additionally swagger2openapi has been tested on a corpus of 34,679 real-world valid Swagger 2.0 definitions from GitHub and SwaggerHub. However, if you have a definition which causes errors in the converter or does not pass validation, please do not hesitate to raise an issue.

License

BSD-3-Clause except the openapi-3.0.json schema, which is taken from the OpenAPI-Specification and the alternative gnostic-3.0.json schema, which is originally from Google Gnostic. Both of these are licensed under the Apache-2 license.

Keywords

FAQs

Last updated on 09 Jan 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc