diff --git a/lib/index.js b/lib/index.js index baa7e5b..b4e29a8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -46,9 +46,7 @@ const register = async function (server, options, next) { const spec = await Parser.validate(api); - spec.basePath = Utils.prefix(spec.basePath || '/', '/'); - - const basePath = Utils.unsuffix(spec.basePath, '/'); + spec.basePath = Utils.unsuffix(Utils.prefix(spec.basePath || '/', '/'), '/'); //Expose plugin api server.expose({ @@ -107,7 +105,7 @@ const register = async function (server, options, next) { //API docs route server.route({ method: 'GET', - path: basePath + docs.path, + path: spec.basePath + docs.path, config: { handler(request, h) { return apiDocument; diff --git a/lib/validators.js b/lib/validators.js index 44c7f59..48bd577 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -140,7 +140,7 @@ const create = function (options = {}) { return this.parameter.type === 'file' ? result.value : result; }; - for (const parameter of operation.parameters) { + for (const parameter of operation.parameters || []) { const validator = makeValidator(parameter, operation.consumes || options.api.consumes); switch (validator.parameter.in) { diff --git a/test/fixtures/defs/minimal.json b/test/fixtures/defs/minimal.json new file mode 100644 index 0000000..7edb903 --- /dev/null +++ b/test/fixtures/defs/minimal.json @@ -0,0 +1,18 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Test a very minimal schema (validating the specs)" + }, + "paths": { + "/pets": { + "get": { + "responses": { + "200": { + "description": "default response" + } + } + } + } + } +} \ No newline at end of file diff --git a/test/test-swaggerize-hapi.js b/test/test-swaggerize-hapi.js index b2980e5..d4562d3 100644 --- a/test/test-swaggerize-hapi.js +++ b/test/test-swaggerize-hapi.js @@ -115,6 +115,33 @@ Test('test plugin', function (t) { } }); + t.test('minimal', async function (t) { + t.plan(1); + + const server = new Hapi.Server(); + + try { + await server.register({ + plugin: Swaggerize, + options: { + api: Path.join(__dirname, './fixtures/defs/minimal.json'), + handlers: Path.join(__dirname, './fixtures/handlers') + } + }); + + let response = await server.inject({ + method: 'GET', + url: '/pets' + }); + + t.strictEqual(response.statusCode, 200, `${response.request.path} OK.`); + + } + catch (error) { + t.fail(error.message); + } + + }); t.test('routes', async function (t) { t.plan(5);