Skip to content

Commit

Permalink
more error details on invalid api fixes APIDevTools#116
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHolly committed Oct 25, 2019
1 parent 2848987 commit 66e58b5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
16 changes: 4 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,11 @@ SwaggerParser.prototype.parse = async function (path, api, options, callback) {
let schema = await $RefParser.prototype.parse.call(this, args.path, args.schema, args.options);

if (schema.swagger) {
// Verify that the parsed object is a Swagger API
if (schema.swagger === undefined || schema.info === undefined || schema.paths === undefined) {
throw ono.syntax(`${args.path || args.schema} is not a valid Swagger API definition`);
}
else if (typeof schema.swagger === "number") {
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") {
else if (schema.info && 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.');
}
Expand All @@ -79,15 +75,11 @@ SwaggerParser.prototype.parse = async function (path, api, options, callback) {
else {
let supportedVersions = ["3.0.0", "3.0.1", "3.0.2"];

// Verify that the parsed object is a Openapi API
if (schema.openapi === undefined || schema.info === undefined || schema.paths === undefined) {
throw ono.syntax(`${args.path || args.schema} is not a valid Openapi API definition`);
}
else if (typeof schema.openapi === "number") {
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") {
else if (schema.info && 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.');
}
Expand Down
2 changes: 1 addition & 1 deletion test/specs/invalid/invalid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("Invalid APIs (can't be parsed)", () => {
}
catch (err) {
expect(err).to.be.an.instanceOf(SyntaxError);
expect(err.message).to.contain("not-swagger.yaml is not a valid Openapi API definition");
expect(err.message).to.contain("Unsupported OpenAPI version: undefined. Swagger Parser only supports versions 3.0.0, 3.0.1, 3.0.2");
}
});

Expand Down

0 comments on commit 66e58b5

Please sign in to comment.