パッケージの詳細

@apidevtools/swagger-parser

APIDevTools9.2mMIT10.1.1

Swagger 2.0 and OpenAPI 3.0 parser and validator for Node and browsers

swagger, openapi, open-api, json

readme

Swagger 2.0 and OpenAPI 3.0 parser/validator

Build Status Coverage Status Tested on APIs.guru

npm Dependencies License Buy us a tree

OS and Browser Compatibility

Online Demo

Features

  • Parses Swagger specs in JSON or YAML format
  • Validates against the Swagger 2.0 schema or OpenAPI 3.0 Schema
  • Resolves all $ref pointers, including external files and URLs
  • Can bundle all your Swagger files into a single file that only has internal $ref pointers
  • Can dereference all $ref pointers, giving you a normal JavaScript object that's easy to work with
  • Tested in Node.js and all modern web browsers on Mac, Windows, and Linux
  • Tested on over 1,500 real-world APIs from Google, Microsoft, Facebook, Spotify, etc.
  • Supports circular references, nested references, back-references, and cross-references
  • Maintains object reference equality — $ref pointers to the same value always resolve to the same object instance

Related Projects

Example

SwaggerParser.validate(myAPI, (err, api) => {
  if (err) {
    console.error(err);
  }
  else {
    console.log("API name: %s, Version: %s", api.info.title, api.info.version);
  }
});

Or use async/await or Promise syntax instead. The following example is the same as above:

try {
  let api = await SwaggerParser.validate(myAPI);
  console.log("API name: %s, Version: %s", api.info.title, api.info.version);
}
catch(err) {
  console.error(err);
}

For more detailed examples, please see the API Documentation

Installation

Install using npm:

npm install @apidevtools/swagger-parser

Usage

When using Swagger Parser in Node.js apps, you'll probably want to use CommonJS syntax:

const SwaggerParser = require("@apidevtools/swagger-parser");

When using a transpiler such as Babel or TypeScript, or a bundler such as Webpack or Rollup, you can use ECMAScript modules syntax instead:

import * as SwaggerParser from '@apidevtools/swagger-parser';

Browser support

Swagger Parser supports recent versions of every major web browser. Older browsers may require Babel and/or polyfills.

To use Swagger Parser in a browser, you'll need to use a bundling tool such as Webpack, Rollup, Parcel, or Browserify. Some bundlers may require a bit of configuration, such as setting browser: true in rollup-plugin-resolve.

API Documentation

Full API documentation is available right here

Contributing

I welcome any contributions, enhancements, and bug-fixes. Open an issue on GitHub and submit a pull request.

Building/Testing

To build/test the project locally on your computer:

  1. Clone this repo
    git clone https://github.com/APIDevTools/swagger-parser.git

  2. Install dependencies
    npm install

  3. Run the build script
    npm run build

  4. Run the tests
    npm test

  5. Check the code coverage
    npm run coverage

License

Swagger Parser is 100% free and open-source, under the MIT license. Use it however you want.

This package is Treeware. If you use it in production, then we ask that you buy the world a tree to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Big Thanks To

Thanks to these awesome companies for their support of Open Source developers ❤

GitHub NPM Coveralls

更新履歴

Change Log

All notable changes will be documented in this file. Swagger Parser adheres to Semantic Versioning.

v10.0.0 (2020-07-10)

Breaking Changes

Other Changes

Full Changelog

v9.0.0 (2020-03-14)

  • Moved Swagger Parser to the @APIDevTools scope on NPM

  • The "swagger-parser" NPM package is now just a wrapper around the scoped "@apidevtools/swagger-parser" package

Full Changelog

v8.0.0 (2019-06-22)

Potentially Breaking Changes

Full Changelog

v7.0.0 (2019-06-12)

Breaking Changes

  • Dropped support for Node 6

  • Updated all code to ES6+ syntax (async/await, template literals, arrow functions, etc.)

  • No longer including a pre-built bundle in the package. such as Webpack, Rollup, Parcel, or Browserify to include Swagger Parser in your app

Other Changes

Full Changelog

v6.0.0 (2018-10-05)

  • Dropped support for Bower, since it has been deprecated

  • Removed the debug dependency

Full Changelog

v5.0.0 (2018-05-25)

  • After months and months of delays, initial support for OpenAPI 3.0 is finally here! A big "Thank You!" to Leo Long for doing the work and submitting PR #88.

Full Changelog

v4.1.0 (2018-04-11)

Full Changelog

v4.0.0 (2017-10-19)

Breaking Changes

  • Update the Swagger 2.0 JSON schema, so it's possible that an API that previously passed validation may no longer pass due to changes in the Swagger schema

  • To reduce the size of this library, it no longer includes polyfills for Promises and TypedArrays, which are natively supported in the latest versions of Node and web browsers. If you need to support older browsers (such as IE9), then just use this Promise polyfill and this TypedArray polyfill.

Minor Changes

Full Changelog

v4.0.0-beta.2 (2016-04-25)

Just one small fix

Fixed issue #13. You can now pass a URL and an object to any method.

SwaggerParser.validate("http://example.com/my-schema.json", mySchemaObject, {})

NOTE: As shown in the example above, you must also pass an options object (even an empty object will work), otherwise, the method signature looks like you're just passing a URL and options.

Full Changelog

v4.0.0-beta.1 (2016-04-10)

Plug-ins !!!

That's the major new feature in this version. Originally requested in PR #8, and refined a few times over the past few months, the plug-in API is now finalized and ready to use. You can now define your own resolvers and parsers.

Breaking Changes

The available options have changed, mostly due to the new plug-in API. There's not a one-to-one mapping of old options to new options, so you'll have to read the docs and determine which options you need to set. If any. The out-of-the-box configuration works for most people.

All of the caching options have been removed. Instead, files are now cached by default, and the entire cache is reset for each new parse operation. Caching options may come back in a future release, if there is enough demand for it. If you used the old caching options, please open an issue and explain your use-case and requirements. I need a better understanding of what caching functionality is actually needed by users.

Bug Fixes

Lots of little bug fixes, and a couple major bug fixes:

Full Changelog

v3.3.0 (2015-10-02)

Updated to the latest version of the Official Swagger 2.0 Schema. The schema hadn't been updated for six months, so Swagger Parser was missing several recent changes.

Full Changelog

v3.2.0 (2015-10-01)

Swagger Parser now uses call-me-maybe to support promises or callbacks.

Full Changelog

v3.1.0 (2015-09-28)

Fixed several bugs with circular references, particularly with the validate() method.

Added a new $refs.circular option to determine how circular references are handled. Options are fully-dereferencing them (default), throwing an error, or ignoring them.

Full Changelog

v3.0.0 (2015-09-25)

This is a complete rewrite of Swagger Parser. Major changes include:

Swagger 2.0 Compliant
Previous versions of Swagger Parser were based on early drafts of Swagger 2.0, and were not compliant with the final version of the spec. Swagger Parser v3.0 is now compliant with the final spec as well as related specs, such as JSON Reference and JSON Pointer

All-New API
The old API only had a single method: parse(). But depending on which options you passed it, the method did much more than its name implied. The new API has separate methods to make things a bit more intuitive. The most commonly used will be validate(), bundle(), and dereference(). The parse() and resolve() methods are also available, but these are mostly just for internal use by the other methods.

Asynchronous API
The old API was "asynchronous", but it relied on global state, so it did not support multiple simultaneous operations. The new API is truly asynchronous and supports both ES6 Promises and Node-style callbacks.

New JSON Schema Validator
Swagger Parser has switched from tv4 to Z-Schema, which is faster and performs more accurate validation. This means that some APIs that previously passed validation will now fail. But that's a good thing!

Full Changelog