From 66e58b5625355891cc5d9c5eaeaf13f429c31dd9 Mon Sep 17 00:00:00 2001 From: Alexander Holland Date: Fri, 25 Oct 2019 04:57:36 +0200 Subject: [PATCH] more error details on invalid api fixes #116 --- lib/index.js | 16 ++++------------ test/specs/invalid/invalid.spec.js | 2 +- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/lib/index.js b/lib/index.js index add44456..47ddb36b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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.'); } @@ -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.'); } diff --git a/test/specs/invalid/invalid.spec.js b/test/specs/invalid/invalid.spec.js index dd42cad5..43cf844a 100644 --- a/test/specs/invalid/invalid.spec.js +++ b/test/specs/invalid/invalid.spec.js @@ -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"); } });