Repository: glennjones/hapi-swagger (js)

Issue:
Make the "api" tag used to filter routes configurable
It would be really useful if you could configure the tag used to filter the routers included in the OpenAPI spec file, when setting up the hapi-swagger plugin. This would enable configuring two docs for different APIs within the same HapiJS server and thus being able to have different descriptions and global attributes.

I could create a PR.

Pull request:
add setting for route tag name. closes #619

Patch:
diff --git a/lib/builder.js b/lib/builder.js
index acb80e7..65544aa 100644
--- a/lib/builder.js
+++ b/lib/builder.js
@@ -19,7 +19,8 @@ const internals = {};
 builder.default = {
   swagger: '2.0',
   host: 'localhost',
-  basePath: '/'
+  basePath: '/',
+  routeTag: 'api',
 };
 
 /**
@@ -86,7 +87,7 @@ builder.getSwaggerJSON = async (settings, request) => {
 
   let routes = request.server.table();
 
-  routes = Filter.byTags(['api'], routes);
+  routes = Filter.byTags([settings.routeTag], routes);
   Sort.paths(settings.sortPaths, routes);
 
   // filter routes displayed based on tags passed in query string
@@ -241,7 +242,8 @@ internals.removeNoneSchemaOptions = function(options) {
     'cache',
     'pathReplacements',
     'log',
-    'cors'
+    'cors',
+    'routeTag',
   ].forEach(element => {
     delete out[element];
   });
diff --git a/lib/index.js b/lib/index.js
index 393e064..48b93dd 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -45,7 +45,8 @@ const schema = Joi.object({
       pattern: Joi.object().type(RegExp),
       replacement: Joi.string().allow('')
     })
-  )
+  ),
+  routeTag: Joi.string(),
 }).unknown();
 
 /**


Test patch:
diff --git a/test/Integration/builder-test.js b/test/Integration/builder-test.js
index 87d9a6f..20c639c 100644
--- a/test/Integration/builder-test.js
+++ b/test/Integration/builder-test.js
@@ -15,6 +15,22 @@ const routes = [
     options: {
       tags: ['api']
     }
+  },
+  {
+    method: 'GET',
+    path: '/test2',
+    handler: Helper.defaultHandler,
+    options: {
+      tags: ['api2']
+    }
+  },
+  {
+    method: 'GET',
+    path: '/not-part-of-api',
+    handler: Helper.defaultHandler,
+    options: {
+      tags: ['other']
+    }
   }
 ];
 
@@ -264,6 +280,62 @@ lab.experiment('builder', () => {
     expect(response.statusCode).to.equal(200);
     expect(response.result.host).to.equal('194.148.15.24');
   });
+
+  lab.test('routeTag : "api"', async () => {
+    const server = await Helper.createServer({ routeTag: 'api' }, routes);
+    const response = await server.inject({ method: 'GET', url: '/swagger.json' });
+
+    expect(response.result.paths['/test']).to.equal({
+      get: {
+          operationId: 'getTest',
+          responses: {
+            default: {
+              description: 'Successful',
+              schema: {
+                type: 'string'
+              }
+            }
+          },
+          tags: [
+            'test'
+          ]
+        }
+      }
+    );
+    expect(response.result.paths['/test2']).to.equal(undefined);
+    expect(response.result.paths['/not-part-of-api']).to.equal(undefined);
+
+    const isValid = await Validate.test(response.result);
+    expect(isValid).to.be.true();
+  });
+
+  lab.test('routeTag : "api2"', async () => {
+    const server = await Helper.createServer({ routeTag: 'api2' }, routes);
+    const response = await server.inject({ method: 'GET', url: '/swagger.json' });
+
+    expect(response.result.paths['/test']).to.equal(undefined);
+    expect(response.result.paths['/test2']).to.equal({
+      get: {
+          operationId: 'getTest2',
+          responses: {
+            default: {
+              description: 'Successful',
+              schema: {
+                type: 'string'
+              }
+            }
+          },
+          tags: [
+            'test2'
+          ]
+        }
+      }
+    );
+    expect(response.result.paths['/not-part-of-api']).to.equal(undefined);
+
+    const isValid = await Validate.test(response.result);
+    expect(isValid).to.be.true();
+  });
 });
 
 lab.experiment('builder', () => {