Skip to content

Commit

Permalink
Feature/Support for custom operation tags (#164)
Browse files Browse the repository at this point in the history
* Enable customTags to be added alongside with the default api tag

* Added tag tests
  • Loading branch information
ggrg authored and tlivings committed Oct 17, 2019
1 parent f53fbab commit d803d48
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ const create = async function (server, { api, basedir, cors, vhost, handlers, ex
continue;
}

const customTags = operation.tags || [];
const options = Object.assign({
cors,
id: operation.operationId,
// hapi does not support empty descriptions
description: operation.description !== '' ? operation.description : undefined,
tags: ['api']
tags: ['api', ...customTags]
}, xoptions);

options.handler = handler;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hapi-openapi",
"description": "Design-driven apis with OpenAPI (formerly Swagger) 2.0 and hapi.",
"version": "1.2.3",
"version": "1.2.4",
"author": "Trevor Livingston <[email protected]>",
"repository": {
"type": "git",
Expand Down
114 changes: 114 additions & 0 deletions test/test-hapi-openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,120 @@ Test('test plugin', function (t) {

});

t.test('hapi operation tags', async function (t) {
t.plan(1);

const server = new Hapi.Server();

const api = {
swagger: '2.0',
info: {
title: 'Minimal',
version: '1.0.0'
},
paths: {
'/test': {
get: {
tags: [
'sample1',
'sample2'
],
responses: {
200: {
description: 'default response'
}
}
}
}
}
};
const expectedTags = ['api', 'sample1', 'sample2']

try {
await server.register({
plugin: OpenAPI,
options: {
api,
handlers: {
test: {
get() {
return 'test';
}
}
}
}
});

let response = await server.inject({
method: 'GET',
url: '/test'
});
const responsteTags = response.request.route.settings.tags

t.deepEqual(responsteTags, expectedTags, 'additional tags successfully configured');

}
catch (error) {
t.fail(error.message);
}

});

t.test('hapi operation tags omitted', async function (t) {
t.plan(1);

const server = new Hapi.Server();

const api = {
swagger: '2.0',
info: {
title: 'Minimal',
version: '1.0.0'
},
paths: {
'/test': {
get: {
responses: {
200: {
description: 'default response'
}
}
}
}
}
};
const expectedDefaultTags = ['api']

try {
await server.register({
plugin: OpenAPI,
options: {
api,
handlers: {
test: {
get() {
return 'test';
}
}
}
}
});

let response = await server.inject({
method: 'GET',
url: '/test'
});
const responsteTags = response.request.route.settings.tags

t.deepEqual(responsteTags, expectedDefaultTags, 'returned default tags');

}
catch (error) {
t.fail(error.message);
}

});

});

Test('multi-register', function (t) {
Expand Down

0 comments on commit d803d48

Please sign in to comment.