Skip to content

Commit 27ac9de

Browse files
committed
add openapi support
1 parent 07575d7 commit 27ac9de

8 files changed

Lines changed: 7718 additions & 6289 deletions

File tree

dist/swagger-parser.js

Lines changed: 6763 additions & 5433 deletions
Large diffs are not rendered by default.

dist/swagger-parser.js.map

Lines changed: 201 additions & 199 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/swagger-parser.min.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/swagger-parser.min.js.map

Lines changed: 700 additions & 631 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,46 @@ SwaggerParser.prototype.parse = function (path, api, options, callback) {
5858

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

63-
// Verify that the parsed object is a Swagger API
64-
if (schema.swagger === undefined || schema.info === undefined || schema.paths === undefined) {
65-
throw ono.syntax('%s is not a valid Swagger API definition', args.path || args.schema);
66-
}
67-
else if (typeof schema.swagger === 'number') {
68-
// This is a very common mistake, so give a helpful error message
69-
throw ono.syntax('Swagger version number must be a string (e.g. "2.0") not a number.');
70-
}
71-
else if (typeof schema.info.version === 'number') {
72-
// This is a very common mistake, so give a helpful error message
73-
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.');
74-
}
75-
else if (supportedSwaggerVersions.indexOf(schema.swagger) === -1) {
76-
throw ono.syntax(
77-
'Unsupported Swagger version: %d. Swagger Parser only supports version %s',
78-
schema.swagger, supportedSwaggerVersions.join(', '));
64+
// Verify that the parsed object is a Swagger API
65+
if (schema.swagger === undefined || schema.info === undefined || schema.paths === undefined) {
66+
throw ono.syntax('%s is not a valid Swagger API definition', args.path || args.schema);
67+
}
68+
else if (typeof schema.swagger === 'number') {
69+
// This is a very common mistake, so give a helpful error message
70+
throw ono.syntax('Swagger version number must be a string (e.g. "2.0") not a number.');
71+
}
72+
else if (typeof schema.info.version === 'number') {
73+
// This is a very common mistake, so give a helpful error message
74+
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.');
75+
}
76+
else if (supportedVersions.indexOf(schema.swagger) === -1) {
77+
throw ono.syntax(
78+
'Unsupported Swagger version: %d. Swagger Parser only supports version %s',
79+
schema.swagger, supportedVersions.join(', '));
80+
}
81+
} else {
82+
var supportedVersions = ['3.0.0', '3.0.1'];
83+
84+
// Verify that the parsed object is a Openapi API
85+
if (schema.openapi === undefined || schema.info === undefined || schema.paths === undefined) {
86+
throw ono.syntax('%s is not a valid Openapi API definition', args.path || args.schema);
87+
}
88+
else if (typeof schema.openapi === 'number') {
89+
// This is a very common mistake, so give a helpful error message
90+
throw ono.syntax('Openapi version number must be a string (e.g. "3.0.0") not a number.');
91+
}
92+
else if (typeof schema.info.version === 'number') {
93+
// This is a very common mistake, so give a helpful error message
94+
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.');
95+
}
96+
else if (supportedVersions.indexOf(schema.openapi) === -1) {
97+
throw ono.syntax(
98+
'Unsupported Openapi version: %d. Openapi Parser only supports version %s',
99+
schema.openapi, supportedVersions.join(', '));
100+
}
79101
}
80102

81103
// Looks good!

lib/validators/schema.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
var util = require('../util'),
44
ono = require('ono'),
55
ZSchema = require('z-schema'),
6-
swaggerSchema = require('swagger-schema-official/schema.json');
6+
swaggerSchema = require('swagger-schema-official/schema.json'),
7+
openapiSchema = require('openapi-schema-validation/schema/openapi-3.0.json');
78

89
module.exports = validateSchema;
910

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

2021
// Validate the API against the Swagger schema
21-
var isValid = ZSchema.validate(api, swaggerSchema);
22+
if (api.swagger) {
23+
var isValid = ZSchema.validate(api, swaggerSchema);
24+
} else {
25+
var isValid = ZSchema.validate(api, openapiSchema);
26+
}
2227

2328
if (isValid) {
2429
util.debug(' Validated successfully');

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@
7878
"debug": "^3.1.0",
7979
"json-schema-ref-parser": "^5.0.3",
8080
"ono": "^4.0.5",
81+
"openapi-schema-validation": "^0.4.1",
8182
"swagger-methods": "^1.0.4",
8283
"swagger-schema-official": "2.0.0-bab6bed",
8384
"z-schema": "^3.19.1"
8485
}
85-
}
86+
}

test/specs/invalid/invalid.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('Invalid APIs (can\'t be parsed)', function () {
77
.then(helper.shouldNotGetCalled)
88
.catch(function (err) {
99
expect(err).to.be.an.instanceOf(SyntaxError);
10-
expect(err.message).to.contain('not-swagger.yaml is not a valid Swagger API definition');
10+
expect(err.message).to.contain('not-swagger.yaml is not a valid Openapi API definition');
1111
});
1212
});
1313

0 commit comments

Comments
 (0)