From 4ec4fe3bdad116ae0ec971eade65dcaaf420b8e9 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Sat, 28 Mar 2020 19:42:45 -0400 Subject: [PATCH 1/9] Upgrade hapi to v19 This upgrade required two main changes: 1. There is no longer a way to query the strategies registered with the server. The previous behavior was to warn that a strategy was not registered, then continue creating the route without the specified auth options. With this change, the route options will be added as long as they are specified. If the auth strategy is not registered, then an error will be thrown when registering the route. This seems to be inline with README. 2. Hapi 19 no longer allows uncompiled top-level validation schemas unless a validator is configured with the server. In previous versions, Joi was used by default. To address this, after the validators are created for a path/operation, any top-level object that is not already a Joi schema is wrapped in Joi.object(). --- README.md | 8 ++++---- lib/routes.js | 5 ----- lib/validators.js | 8 +++++++- package.json | 2 +- test/test-auth.js | 10 +++++++++- test/test-forms.js | 2 +- test/test-hapi-openapi.js | 2 +- test/test-validators.js | 2 +- test/test_xoptions.js | 2 +- 9 files changed, 25 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 333ff96..f232e7d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # hapi-openapi -[![Build Status](https://travis-ci.org/krakenjs/hapi-openapi.svg?branch=master)](https://travis-ci.org/krakenjs/hapi-openapi) -[![NPM version](https://badge.fury.io/js/hapi-openapi.png)](http://badge.fury.io/js/hapi-openapi) +[![Build Status](https://travis-ci.org/krakenjs/hapi-openapi.svg?branch=master)](https://travis-ci.org/krakenjs/hapi-openapi) +[![NPM version](https://badge.fury.io/js/hapi-openapi.png)](http://badge.fury.io/js/hapi-openapi) ### Note: this project was renamed from 'swaggerize-hapi' to 'hapi-openapi'. @@ -49,7 +49,7 @@ You now have a working api and can use something like [SwaggerHub](https://swagg ### Manual Usage ```javascript -const Hapi = require('hapi'); +const Hapi = require('@hapi/hapi'); const server = new Hapi.Server(); @@ -194,7 +194,7 @@ This will construct a `handlers` object from the given `x-hapi-handler` files. ### X-Hapi-Options -There is now support at the operations level for `x-hapi-options` which represent individual [Hapi Route Optijons](https://github.com/hapijs/hapi/blob/master/API.md#route-options). +There is now support at the operations level for `x-hapi-options` which represent individual [Hapi Route Optijons](https://github.com/hapijs/hapi/blob/master/API.md#route-options). This support is limited to configuration supported by the JSON file type. diff --git a/lib/routes.js b/lib/routes.js index d55d90e..26889f0 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -94,11 +94,6 @@ const create = async function (server, { api, basedir, cors, vhost, handlers, ex const securitySchemes = Object.keys(secdef); for (const securityDefinitionName of securitySchemes) { - if (!server.auth._strategies[securityDefinitionName]) { - server.log(['warn'], `${securityDefinitionName} strategy was not registered.`); - continue; - } - const securityDefinition = api.securityDefinitions[securityDefinitionName]; Hoek.assert(securityDefinition, 'Security scheme not defined.'); diff --git a/lib/validators.js b/lib/validators.js index 52e48e1..7f2a8d4 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -140,7 +140,7 @@ const create = function (options = {}) { const formValidator = async function (value) { const result = await this.validate(value); - + if (result.error) { throw result.error; } @@ -193,6 +193,12 @@ const create = function (options = {}) { } } + for (const [key, value] of Object.entries(validate)) { + if (typeof value === 'object' && !value.isJoi) { + validate[key] = Joi.object(value); + } + } + return { validate, routeExt diff --git a/package.json b/package.json index 8af2a40..220e27a 100644 --- a/package.json +++ b/package.json @@ -24,11 +24,11 @@ "swagger-parser": "^4.1.0" }, "devDependencies": { + "@hapi/hapi": "^19.1.1", "boom": "^7.2.0", "eslint": "^4.19.1", "eslint-config-hapi": "^11.1.0", "eslint-plugin-hapi": "^4.1.0", - "hapi": "^17.4.0", "nyc": "^13.3.0", "tape": "^4.9.0" }, diff --git a/test/test-auth.js b/test/test-auth.js index 4eea88c..7a21322 100644 --- a/test/test-auth.js +++ b/test/test-auth.js @@ -3,7 +3,7 @@ const Test = require('tape'); const Path = require('path'); const OpenAPI = require('../lib'); -const Hapi = require('hapi'); +const Hapi = require('@hapi/hapi'); const StubAuthTokenScheme = require('./fixtures/lib/stub-auth-token-scheme'); Test('authentication', function (t) { @@ -80,6 +80,10 @@ Test('authentication', function (t) { } }); + server.auth.strategy('api_key2', 'stub-auth-token', { + validateFunc: () => ({ isValid: true }) + }); + await server.register({ plugin: OpenAPI, options: { @@ -117,6 +121,10 @@ Test('authentication', function (t) { } }); + server.auth.strategy('api_key2', 'stub-auth-token', { + validateFunc: () => ({ isValid: true }) + }); + await server.register({ plugin: OpenAPI, options: { diff --git a/test/test-forms.js b/test/test-forms.js index 1a1400e..d8b8fa6 100644 --- a/test/test-forms.js +++ b/test/test-forms.js @@ -3,7 +3,7 @@ const Test = require('tape'); const Path = require('path'); const OpenAPI = require('../lib'); -const Hapi = require('hapi'); +const Hapi = require('@hapi/hapi'); Test('form data', function (t) { diff --git a/test/test-hapi-openapi.js b/test/test-hapi-openapi.js index b026f92..7c0e181 100644 --- a/test/test-hapi-openapi.js +++ b/test/test-hapi-openapi.js @@ -3,7 +3,7 @@ const Test = require('tape'); const Path = require('path'); const OpenAPI = require('../lib'); -const Hapi = require('hapi'); +const Hapi = require('@hapi/hapi'); Test('test plugin', function (t) { diff --git a/test/test-validators.js b/test/test-validators.js index de69582..e9abbd8 100644 --- a/test/test-validators.js +++ b/test/test-validators.js @@ -88,7 +88,7 @@ Test('validator special types', function(t) { const v = validator.makeAll(api.paths['/test/{foo*}'].get.parameters); - const keys = Object.keys(v.validate.params); + const keys = Object.keys(v.validate.params.describe().children); if (keys.length === 1 && keys[0] === 'foo') { return t.pass(`${keys.join(', ')} are valid.`); diff --git a/test/test_xoptions.js b/test/test_xoptions.js index 272d5b6..2872bb5 100644 --- a/test/test_xoptions.js +++ b/test/test_xoptions.js @@ -3,7 +3,7 @@ const Test = require('tape'); const Path = require('path'); const OpenAPI = require('../lib'); -const Hapi = require('hapi'); +const Hapi = require('@hapi/hapi'); Test('x-hapi-options', function (t) { From 9a4121c11cdbe1e7042ca86516454ddbccb98fb9 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Sat, 28 Mar 2020 20:49:09 -0400 Subject: [PATCH 2/9] Upgrade joi/enjoi Upgrade enjoi to latest and joi to latest that is supported by enjoi. Additional work needs to be done once enjoi supports @hapi/joi >= 16. --- lib/index.js | 4 ++-- lib/validators.js | 2 +- package.json | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 4da6801..40616db 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,7 @@ 'use strict'; const Package = require('../package.json'); -const Joi = require('joi'); +const Joi = require('@hapi/joi'); const Hoek = require('hoek'); const Caller = require('./caller'); const Path = require('path'); @@ -164,7 +164,7 @@ const register = async function (server, options, next) { }, vhost }); - + const routes = await Routes.create(server, { api: spec, basedir, cors, vhost, handlers, extensions, outputvalidation }); for (const route of routes) { diff --git a/lib/validators.js b/lib/validators.js index 7f2a8d4..5a28b97 100644 --- a/lib/validators.js +++ b/lib/validators.js @@ -1,7 +1,7 @@ 'use strict'; const Enjoi = require('enjoi'); -const Joi = require('joi'); +const Joi = require('@hapi/joi'); const Util = require('util'); const types = { diff --git a/package.json b/package.json index 220e27a..7365fed 100644 --- a/package.json +++ b/package.json @@ -15,10 +15,10 @@ "node": ">=8" }, "dependencies": { + "@hapi/joi": "^15.1.1", "dot-prop": "^4.2.0", - "enjoi": "^4.0.0", + "enjoi": "^6.0.1", "hoek": "^5.0.3", - "joi": "^13.6.0", "js-yaml": "^3.11.0", "merge-object-files": "^2.0.0", "swagger-parser": "^4.1.0" From a76ae471e074d5e55e30c16063597842ab67b84f Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Sat, 28 Mar 2020 20:53:20 -0400 Subject: [PATCH 3/9] Upgrade hoek to 9.0.4 --- lib/index.js | 2 +- lib/routes.js | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 40616db..0a753f5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2,7 +2,7 @@ const Package = require('../package.json'); const Joi = require('@hapi/joi'); -const Hoek = require('hoek'); +const Hoek = require('@hapi/hoek'); const Caller = require('./caller'); const Path = require('path'); const Parser = require('swagger-parser'); diff --git a/lib/routes.js b/lib/routes.js index 26889f0..bcd7288 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -2,7 +2,7 @@ const ObjectFiles = require('merge-object-files'); const Validators = require('./validators'); -const Hoek = require('hoek'); +const Hoek = require('@hapi/hoek'); const Utils = require('./utils'); const Path = require('path'); const Props = require('dot-prop'); diff --git a/package.json b/package.json index 7365fed..548db44 100644 --- a/package.json +++ b/package.json @@ -15,10 +15,10 @@ "node": ">=8" }, "dependencies": { + "@hapi/hoek": "^9.0.4", "@hapi/joi": "^15.1.1", "dot-prop": "^4.2.0", "enjoi": "^6.0.1", - "hoek": "^5.0.3", "js-yaml": "^3.11.0", "merge-object-files": "^2.0.0", "swagger-parser": "^4.1.0" From 0666ac0c6a7f14c3a4bc09ee30135bba84a92284 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Sat, 28 Mar 2020 20:56:24 -0400 Subject: [PATCH 4/9] Upgrade swagger-parser to 9.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 548db44..2df5d6f 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,7 @@ "enjoi": "^6.0.1", "js-yaml": "^3.11.0", "merge-object-files": "^2.0.0", - "swagger-parser": "^4.1.0" + "swagger-parser": "^9.0.1" }, "devDependencies": { "@hapi/hapi": "^19.1.1", From 0390d951f560a71d010c87f52dd068bfbbc4f709 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Sat, 28 Mar 2020 21:07:25 -0400 Subject: [PATCH 5/9] Upgrade boom to 9.1.0 --- README.md | 2 +- package.json | 2 +- test/fixtures/lib/stub-auth-token-scheme.js | 2 +- test/fixtures/lib/xauth-scheme.js | 2 +- test/fixtures/lib/xauth-strategy.js | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f232e7d..0335d11 100644 --- a/README.md +++ b/README.md @@ -321,7 +321,7 @@ and ```javascript //xauth-strategy.js -const Boom = require('boom'); +const Boom = require('@hapi/boom'); const register = function (server, { name, scheme, where, lookup }) { server.auth.strategy(name, /* the scheme to use this strategy with */ scheme, { diff --git a/package.json b/package.json index 2df5d6f..7801fbf 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,8 @@ "swagger-parser": "^9.0.1" }, "devDependencies": { + "@hapi/boom": "^9.1.0", "@hapi/hapi": "^19.1.1", - "boom": "^7.2.0", "eslint": "^4.19.1", "eslint-config-hapi": "^11.1.0", "eslint-plugin-hapi": "^4.1.0", diff --git a/test/fixtures/lib/stub-auth-token-scheme.js b/test/fixtures/lib/stub-auth-token-scheme.js index 7cfc9cc..698c222 100644 --- a/test/fixtures/lib/stub-auth-token-scheme.js +++ b/test/fixtures/lib/stub-auth-token-scheme.js @@ -1,6 +1,6 @@ 'use strict'; -var Boom = require('boom'); +var Boom = require('@hapi/boom'); const register = function (server, options) { server.auth.scheme('stub-auth-token', function (server, options) { diff --git a/test/fixtures/lib/xauth-scheme.js b/test/fixtures/lib/xauth-scheme.js index e53fb38..ffc90be 100644 --- a/test/fixtures/lib/xauth-scheme.js +++ b/test/fixtures/lib/xauth-scheme.js @@ -1,6 +1,6 @@ 'use strict'; -var Boom = require('boom'); +var Boom = require('@hapi/boom'); const register = function (server, { name }) { server.auth.scheme(name /*apiKey*/, (server, { validate }) => { diff --git a/test/fixtures/lib/xauth-strategy.js b/test/fixtures/lib/xauth-strategy.js index 9574839..814d6e3 100644 --- a/test/fixtures/lib/xauth-strategy.js +++ b/test/fixtures/lib/xauth-strategy.js @@ -1,6 +1,6 @@ 'use strict'; -const Boom = require('boom'); +const Boom = require('@hapi/boom'); const register = function (server, { name, scheme, where, lookup }) { server.auth.strategy(name, scheme, { From 932357e13fb6ca2247010f492322469a68f6b1c7 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Sat, 28 Mar 2020 21:08:08 -0400 Subject: [PATCH 6/9] Upgrade nyc This upgrade addresses a vulnerability found by npm audit --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7801fbf..ae0d395 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "eslint": "^4.19.1", "eslint-config-hapi": "^11.1.0", "eslint-plugin-hapi": "^4.1.0", - "nyc": "^13.3.0", + "nyc": "^15.0.0", "tape": "^4.9.0" }, "scripts": { From a5b462e7750af27e077bc7d4a395b00020739c54 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Thu, 2 Apr 2020 19:45:40 -0400 Subject: [PATCH 7/9] Downgrade @hapi/joi to v14 This aligns @hapi/joi with the peer dependency requirement for enjoi. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ae0d395..58040b5 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ }, "dependencies": { "@hapi/hoek": "^9.0.4", - "@hapi/joi": "^15.1.1", + "@hapi/joi": "^14.5.0", "dot-prop": "^4.2.0", "enjoi": "^6.0.1", "js-yaml": "^3.11.0", From c6b6397f2b3d20833402e4f28c2d2b9d045febf8 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Fri, 3 Apr 2020 13:13:55 -0400 Subject: [PATCH 8/9] Update package.json to reflect node >= 12 requirement Hapi v19 requires node >= 12. This change will make that more apparent to consumers. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 58040b5..aee8f11 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "registry": "https://registry.npmjs.org" }, "engines": { - "node": ">=8" + "node": ">=12" }, "dependencies": { "@hapi/hoek": "^9.0.4", From ed9eab2ec5da04fc43b5cc352ed3dbf1ddb7bd28 Mon Sep 17 00:00:00 2001 From: Tim Burch Date: Fri, 3 Apr 2020 13:31:06 -0400 Subject: [PATCH 9/9] Remove node 8 & 10 from travis --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0c91c83..665b195 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: node_js node_js: - - "8" - - "10" + - "12" - "stable" branches: only: