Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add openapi support #88

Merged
merged 1 commit into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12,196 changes: 6,763 additions & 5,433 deletions dist/swagger-parser.js

Large diffs are not rendered by default.

400 changes: 201 additions & 199 deletions dist/swagger-parser.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions dist/swagger-parser.min.js

Large diffs are not rendered by default.

1,331 changes: 700 additions & 631 deletions dist/swagger-parser.min.js.map

Large diffs are not rendered by default.

56 changes: 39 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,46 @@ SwaggerParser.prototype.parse = function (path, api, options, callback) {

return $RefParser.prototype.parse.call(this, args.path, args.schema, args.options)
.then(function (schema) {
var supportedSwaggerVersions = ['2.0'];
if (schema.swagger) {
var supportedVersions = ['2.0'];

// Verify that the parsed object is a Swagger API
if (schema.swagger === undefined || schema.info === undefined || schema.paths === undefined) {
throw ono.syntax('%s is not a valid Swagger API definition', args.path || args.schema);
}
else if (typeof schema.swagger === 'number') {
// This is a very common mistake, so give a helpful error message
throw ono.syntax('Swagger version number must be a string (e.g. "2.0") not a number.');
}
else if (typeof schema.info.version === 'number') {
// This is a very common mistake, so give a helpful error message
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.');
}
else if (supportedSwaggerVersions.indexOf(schema.swagger) === -1) {
throw ono.syntax(
'Unsupported Swagger version: %d. Swagger Parser only supports version %s',
schema.swagger, supportedSwaggerVersions.join(', '));
// Verify that the parsed object is a Swagger API
if (schema.swagger === undefined || schema.info === undefined || schema.paths === undefined) {
throw ono.syntax('%s is not a valid Swagger API definition', args.path || args.schema);
}
else if (typeof schema.swagger === 'number') {
// This is a very common mistake, so give a helpful error message
throw ono.syntax('Swagger version number must be a string (e.g. "2.0") not a number.');
}
else if (typeof schema.info.version === 'number') {
// This is a very common mistake, so give a helpful error message
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.');
}
else if (supportedVersions.indexOf(schema.swagger) === -1) {
throw ono.syntax(
'Unsupported Swagger version: %d. Swagger Parser only supports version %s',
schema.swagger, supportedVersions.join(', '));
}
} else {
var supportedVersions = ['3.0.0', '3.0.1'];

// Verify that the parsed object is a Openapi API
if (schema.openapi === undefined || schema.info === undefined || schema.paths === undefined) {
throw ono.syntax('%s is not a valid Openapi API definition', args.path || args.schema);
}
else if (typeof schema.openapi === 'number') {
// This is a very common mistake, so give a helpful error message
throw ono.syntax('Openapi version number must be a string (e.g. "3.0.0") not a number.');
}
else if (typeof schema.info.version === 'number') {
// This is a very common mistake, so give a helpful error message
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.');
}
else if (supportedVersions.indexOf(schema.openapi) === -1) {
throw ono.syntax(
'Unsupported Openapi version: %d. Openapi Parser only supports version %s',
schema.openapi, supportedVersions.join(', '));
}
}

// Looks good!
Expand Down
9 changes: 7 additions & 2 deletions lib/validators/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
var util = require('../util'),
ono = require('ono'),
ZSchema = require('z-schema'),
swaggerSchema = require('swagger-schema-official/schema.json');
swaggerSchema = require('swagger-schema-official/schema.json'),
openapiSchema = require('openapi-schema-validation/schema/openapi-3.0.json');

module.exports = validateSchema;

Expand All @@ -18,7 +19,11 @@ function validateSchema (api) {
util.debug('Validating against the Swagger 2.0 schema');

// Validate the API against the Swagger schema
var isValid = ZSchema.validate(api, swaggerSchema);
if (api.swagger) {
var isValid = ZSchema.validate(api, swaggerSchema);
} else {
var isValid = ZSchema.validate(api, openapiSchema);
}

if (isValid) {
util.debug(' Validated successfully');
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@
"debug": "^3.1.0",
"json-schema-ref-parser": "^5.0.3",
"ono": "^4.0.5",
"openapi-schema-validation": "^0.4.1",
"swagger-methods": "^1.0.4",
"swagger-schema-official": "2.0.0-bab6bed",
"z-schema": "^3.19.1"
}
}
}
2 changes: 1 addition & 1 deletion test/specs/invalid/invalid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Invalid APIs (can\'t be parsed)', function () {
.then(helper.shouldNotGetCalled)
.catch(function (err) {
expect(err).to.be.an.instanceOf(SyntaxError);
expect(err.message).to.contain('not-swagger.yaml is not a valid Swagger API definition');
expect(err.message).to.contain('not-swagger.yaml is not a valid Openapi API definition');
});
});

Expand Down