What is swagger-parser?
swagger-parser is a powerful npm package that allows you to parse, validate, and dereference Swagger (OpenAPI) definitions. It helps in ensuring that your API definitions are correct and can be used to resolve $ref pointers to simplify the API documentation.
What are swagger-parser's main functionalities?
Parse
This feature allows you to parse a Swagger (OpenAPI) definition file. The code sample demonstrates how to parse a Swagger file and log the API name and version.
const SwaggerParser = require('swagger-parser');
SwaggerParser.parse('path/to/your/swagger.yaml')
.then(api => {
console.log('API name: %s, Version: %s', api.info.title, api.info.version);
})
.catch(err => {
console.error(err);
});
Validate
This feature allows you to validate a Swagger (OpenAPI) definition file. The code sample demonstrates how to validate a Swagger file and log whether the API is valid or not.
const SwaggerParser = require('swagger-parser');
SwaggerParser.validate('path/to/your/swagger.yaml')
.then(api => {
console.log('API is valid:', api);
})
.catch(err => {
console.error('API is invalid:', err);
});
Dereference
This feature allows you to dereference $ref pointers in a Swagger (OpenAPI) definition file. The code sample demonstrates how to dereference a Swagger file and log the dereferenced API.
const SwaggerParser = require('swagger-parser');
SwaggerParser.dereference('path/to/your/swagger.yaml')
.then(api => {
console.log('Dereferenced API:', api);
})
.catch(err => {
console.error(err);
});
Bundle
This feature allows you to bundle all external $ref pointers into a single file. The code sample demonstrates how to bundle a Swagger file and log the bundled API.
const SwaggerParser = require('swagger-parser');
SwaggerParser.bundle('path/to/your/swagger.yaml')
.then(api => {
console.log('Bundled API:', api);
})
.catch(err => {
console.error(err);
});
Other packages similar to swagger-parser
openapi-schema-validator
openapi-schema-validator is a package that validates OpenAPI 3.0 schemas. It focuses on schema validation and does not provide parsing or dereferencing functionalities like swagger-parser.
swagger-jsdoc
swagger-jsdoc is a package that generates Swagger (OpenAPI) definitions from JSDoc comments in your code. It is more focused on generating documentation from code comments rather than parsing and validating existing Swagger files.
swagger-client
swagger-client is a package that provides a JavaScript client for interacting with Swagger (OpenAPI) APIs. It includes functionalities for making API requests and handling responses, but it does not focus on parsing or validating Swagger definitions.
Swagger-Parser
Parses, validates, and dereferences your Swagger specs
Features
- Supports Swagger specs in JSON or YAML format
- Validates against the official Swagger 2.0 schema
- Dereferences all $ref pointers, including pointers to external URLs
- Nested $ref pointers are supported, even in external URLs
- Multiple $ref pointers to the same definition are resolved to the same object instance, thus maintaining strict reference equality
Basic Example
swagger.parser.parse("path/to/my/swagger.yaml", function(err, swagger) {
if (err) {
console.error("There's an error in the Swagger file: " + err.message);
return;
}
console.log("Your API is " + swagger.info.title + ", version " + swagger.info.version);
});
The swagger
parameter that is passed to the callback function is a fully-parsed, validated, and dereferenced Swagger Object.
Installation and Use
Node
npm install swagger-parser
Then add this to your Node script:
var parser = require("swagger-parser");
parser.parse('path/to/my/swagger.yaml', function(err, swagger) { ... });
Bower
bower install swagger-parser
Then add this to your HTML page:
<script src="bower_components/swagger-parser/dist/swagger-parser.js"></script>
<script>
swagger.parser.parse('http://mysite.com/path/to/my/swagger.yaml', function(err, swagger) { ... });
</script>
AMD (Require.js)
Just add swagger-parser
to your AMD module's dependencies, or require("swagger-parser")
explicitly.
define("myModule", ["swagger-parser"], function(parser) {
parser.parse('http://mysite.com/path/to/my/swagger.yaml', function(err, swagger) { ... });
});
Options
The parse
function accepts an optional options
parameter, like this:
var options = {
dereferencePointers: false,
validateSpec: false
};
parser.parse("path/to/my/swagger.yaml", options, function(err, swaggerObject) {
...
});
Available options are as follows:
-
parseYaml (default: true) -
Determines whether the parser will allow Swagger specs in YAML format. If set to false
, then only JSON will be allowed.
-
dereferencePointers (default: true) -
Determines whether $ref
pointers will be dereferenced. If set to false
, then the resulting SwaggerObject will contain Reference Objects instead of the objects they reference.
-
dereferenceExternalPointers (default: true) -
Determines whether $ref
pointers will be dereferenced if they point to external files (e.g. "http://company.com/my/schema.yaml"). If set to false
then the resulting SwaggerObject will contain Reference Objects for external pointers instead of the objects they reference.
-
validateSpec (default: true) -
Determines whether your Swagger spec will be validated against the official Swagger schema. If set to false
, then the resulting Swagger Object may be missing properties, have properties of the wrong data type, etc.
Contributing
I welcome any contributions, enhancements, and bug-fixes. File an issue on GitHub and submit a pull request. Use JSHint to make sure your code passes muster. (see .jshintrc).
Here are some things currently on the to-do list:
- Recursive (circular) $ref pointers - Recursive (circular)
$ref
pointers are not currently supported. So something like this won't work:
person:
properties:
name:
type: string
spouse:
type:
$ref: person
License
Swagger-Parser is 100% free and open-source, under the MIT license. Use it however you want.