Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Support for custom operation tags #164

Merged
merged 2 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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