Skip to content

Commit

Permalink
Upgrade dependencies to support hapi 19 (#173)
Browse files Browse the repository at this point in the history
* 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().

* 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.

* Upgrade hoek to 9.0.4

* Upgrade swagger-parser to 9.0.1

* Upgrade boom to 9.1.0

* Upgrade nyc

This upgrade addresses a vulnerability found by npm audit

* Downgrade @hapi/joi to v14

This aligns @hapi/joi with the peer dependency requirement for enjoi.

* Update package.json to reflect node >= 12 requirement

Hapi v19 requires node >= 12. This change will make that more apparent
to consumers.

* Remove node 8 & 10 from travis

Co-authored-by: Tim Burch <[email protected]>
  • Loading branch information
tpburch and Tim Burch authored Apr 5, 2020
1 parent 429bfbf commit d18bbb5
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 34 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: node_js
node_js:
- "8"
- "10"
- "12"
- "stable"
branches:
only:
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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'.

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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, {
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const Package = require('../package.json');
const Joi = require('joi');
const Hoek = require('hoek');
const Joi = require('@hapi/joi');
const Hoek = require('@hapi/hoek');
const Caller = require('./caller');
const Path = require('path');
const Parser = require('swagger-parser');
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 1 addition & 6 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -95,11 +95,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.');
Expand Down
10 changes: 8 additions & 2 deletions lib/validators.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -141,7 +141,7 @@ const create = function (options = {}) {

const formValidator = async function (value) {
const result = await this.validate(value);

if (result.error) {
throw result.error;
}
Expand Down Expand Up @@ -194,6 +194,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
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
"registry": "https://registry.npmjs.org"
},
"engines": {
"node": ">=8"
"node": ">=12"
},
"dependencies": {
"@hapi/hoek": "^9.0.4",
"@hapi/joi": "^14.5.0",
"dot-prop": "^4.2.0",
"enjoi": "^4.0.0",
"hoek": "^5.0.3",
"joi": "^13.6.0",
"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": {
"boom": "^7.2.0",
"@hapi/boom": "^9.1.0",
"@hapi/hapi": "^19.1.1",
"eslint": "^4.19.1",
"eslint-config-hapi": "^11.1.0",
"eslint-plugin-hapi": "^4.1.0",
"hapi": "^17.4.0",
"nyc": "^13.3.0",
"nyc": "^15.0.0",
"tape": "^4.9.0"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/lib/stub-auth-token-scheme.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/lib/xauth-scheme.js
Original file line number Diff line number Diff line change
@@ -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 }) => {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/lib/xauth-strategy.js
Original file line number Diff line number Diff line change
@@ -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, {
Expand Down
10 changes: 9 additions & 1 deletion test/test-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion test/test-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/test-hapi-openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
2 changes: 1 addition & 1 deletion test/test-validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down
2 changes: 1 addition & 1 deletion test/test_xoptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d18bbb5

Please sign in to comment.