Repository: glennjones/hapi-swagger
Pull request #395 (2017-02-15 05:02:39)

Added a grouping setting to choose between path and tags grouping
This will allow us to group endpoints in the swagger UI based on the hapi controller tags


Patch:
diff --git a/lib/builder.js b/lib/builder.js
index f08b322..bb21b04 100644
--- a/lib/builder.js
+++ b/lib/builder.js
@@ -43,6 +43,7 @@ builder.schema = Joi.object({
     responses: Joi.any(),
     securityDefinitions: Joi.any(),
     security: Joi.any(),
+    grouping: Joi.string().valid(['path', 'tags']),
     tags: Joi.any(),
     cors: Joi.boolean(),
     externalDocs: Joi.object({
@@ -233,6 +234,7 @@ internals.removeNoneSchemaOptions = function (options) {
         'sortTags',
         'sortEndpoints',
         'sortPaths',
+        'grouping',
         'xProperties',
         'reuseDefinitions',
         'uiCompleteScript',
diff --git a/lib/defaults.js b/lib/defaults.js
index 02067f9..e79c334 100644
--- a/lib/defaults.js
+++ b/lib/defaults.js
@@ -15,6 +15,7 @@ module.exports = {
     'sortTags': 'default',
     'sortEndpoints': 'path',
     'sortPaths': 'unsorted',
+    'grouping': 'path',
     'uiCompleteScript': null,
     'xProperties': true,
     'reuseDefinitions': true,
diff --git a/lib/paths.js b/lib/paths.js
index f34735b..4d591af 100644
--- a/lib/paths.js
+++ b/lib/paths.js
@@ -190,7 +190,15 @@ internals.paths.prototype.buildRoutes = function (routes) {
         };
 
         // tags in swagger are used for grouping
-        out.tags = route.groups;
+        if (this.settings.grouping === 'tags') {
+            out.tags = route.tags.slice(0);
+            while (out.tags.indexOf('api') !== -1) {
+                out.tags.splice(out.tags.indexOf('api'), 1);
+            }
+        }
+        else {
+            out.tags = route.groups;
+        }
 
         out.description = Array.isArray(route.notes) ? route.notes.join('<br/><br/>') : route.notes;
 


Test patch:
diff --git a/test/Integration/grouping-test.js b/test/Integration/grouping-test.js
new file mode 100644
index 0000000..85ca0a9
--- /dev/null
+++ b/test/Integration/grouping-test.js
@@ -0,0 +1,157 @@
+'use strict';
+const Code = require('code');
+const Hapi = require('hapi');
+const Inert = require('inert');
+const Lab = require('lab');
+const Vision = require('vision');
+const HapiSwagger = require('../../lib/index.js');
+const Helper = require('../helper.js');
+
+const expect = Code.expect;
+const lab = exports.lab = Lab.script();
+
+
+let testPlugin = function (plugin, options, next) {
+
+    plugin.route({
+        method: 'GET',
+        path: '/grouping1',
+        config: {
+            handler: function (request, reply) {
+                reply('Hi from grouping 1');
+            },
+            description: 'plugin1',
+            tags: ['api', 'hello group']
+        }
+    });
+
+    next();
+};
+testPlugin.attributes = { name: 'grouping1' };
+
+let swaggerOptions = {
+    schemes: ['http'],
+    info: {
+        'title': 'Test API Documentation',
+        'description': 'This is a sample example of API documentation.',
+        'version': '1.0.0',
+        'termsOfService': 'https://github.com/glennjones/hapi-swagger/',
+        'contact': {
+            'email': 'glennjonesnet@gmail.com'
+        },
+        'license': {
+            'name': 'MIT',
+            'url': 'https://raw.githubusercontent.com/glennjones/hapi-swagger/master/license.txt'
+        }
+    },
+    connectionLabel: 'api'
+};
+
+lab.experiment('default grouping', () => {
+
+    const server = new Hapi.Server();
+
+    lab.before(function (done) {
+
+        server.connection({ host: 'localhost', port: 3000, labels: 'api' });
+        server.connection({ host: 'localhost', port: 3001, labels: 'docs' });
+        server.register([
+            Inert,
+            Vision,
+            {
+                register: testPlugin,
+                select: ['api']
+            },
+            {
+                register: HapiSwagger,
+                options: swaggerOptions,
+                select: ['docs']
+            }
+        ]);
+
+        server.start(() => { done(); });
+    });
+
+    lab.test('group by path', (done) => {
+
+        const connection = server.select('docs');
+        connection.inject({ method: 'GET', url: '/swagger.json' }, function (response) {
+            expect(response.statusCode).to.equal(200);
+            expect(response.result.host).to.equal('localhost:3000');
+            expect(response.result.paths['/grouping1']).to.equal({
+
+                'get': {
+                    'tags': [
+                        'grouping1'
+                    ],
+                    'responses': {
+                        'default': {
+                            'schema': {
+                                'type': 'string'
+                            },
+                            'description': 'Successful'
+                        }
+                    },
+                    'operationId': 'getGrouping1',
+                    'summary': 'plugin1'
+                }
+            });
+            Helper.validate(response, done, expect);
+        });
+    });
+});
+
+lab.experiment('tag grouping', () => {
+
+    const server = new Hapi.Server();
+
+    lab.before(function (done) {
+
+        swaggerOptions.grouping = 'tags';
+        server.connection({ host: 'localhost', port: 3000, labels: 'api' });
+        server.connection({ host: 'localhost', port: 3001, labels: 'docs' });
+        server.register([
+            Inert,
+            Vision,
+            {
+                register: testPlugin,
+                select: ['api']
+            },
+            {
+                register: HapiSwagger,
+                options: swaggerOptions,
+                select: ['docs']
+            }
+        ]);
+
+        server.start(() => { done(); });
+    });
+
+    lab.test('group by tags', (done) => {
+
+        const connection = server.select('docs');
+        connection.inject({ method: 'GET', url: '/swagger.json' }, function (response) {
+            expect(response.statusCode).to.equal(200);
+            expect(response.result.host).to.equal('localhost:3000');
+            expect(response.result.paths['/grouping1']).to.equal({
+
+                'get': {
+                    'tags': [
+                        'hello group'
+                    ],
+                    'responses': {
+                        'default': {
+                            'schema': {
+                                'type': 'string'
+                            },
+                            'description': 'Successful'
+                        }
+                    },
+                    'operationId': 'getGrouping1',
+                    'summary': 'plugin1'
+                }
+            });
+            Helper.validate(response, done, expect);
+        });
+    });
+});