Repository: glennjones/hapi-swagger
Pull request #719 (2021-06-06 01:15:42)

Add support for tryItOutEnabled option
@robmcguinness something like this? Do you think I should add other missing display options or not bother?


Patch:
diff --git a/index.d.ts b/index.d.ts
index 556622b..0b81a84 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -371,6 +371,12 @@ declare namespace hapiswagger {
      * @default 'https://online.swagger.io/validator'
      */
     validatorUrl?: string;
+
+    /**
+     * Controls whether the "Try it out" section should be enabled by default
+     * @default false
+     */
+    tryItOutEnabled?: string;
   }
 }
 
diff --git a/lib/builder.js b/lib/builder.js
index 200e867..cd9acdd 100644
--- a/lib/builder.js
+++ b/lib/builder.js
@@ -246,7 +246,8 @@ internals.removeNoneSchemaOptions = function(options) {
     'log',
     'cors',
     'routeTag',
-    'validate'
+    'validate',
+    'tryItOutEnabled',
   ].forEach(element => {
     delete out[element];
   });
diff --git a/lib/defaults.js b/lib/defaults.js
index b8d0ca3..d372aa3 100644
--- a/lib/defaults.js
+++ b/lib/defaults.js
@@ -28,5 +28,6 @@ module.exports = {
   deReference: false,
   validatorUrl: '//online.swagger.io/validator',
   acceptToProduce: true, // internal, NOT public
-  pathReplacements: []
+  pathReplacements: [],
+  tryItOutEnabled: false,
 };
diff --git a/lib/index.js b/lib/index.js
index 8cb86ac..5d180d3 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -76,7 +76,8 @@ const schema = Joi.object({
     errorFields: Joi.object(),
     options: Joi.object(),
     validator: Joi.object()
-  })
+  }),
+  tryItOutEnabled: Joi.boolean(),
 }).unknown();
 
 /**
diff --git a/optionsreference.md b/optionsreference.md
index 23b73dc..ac3a114 100644
--- a/optionsreference.md
+++ b/optionsreference.md
@@ -67,6 +67,7 @@
     -   `ordered`: sort by `order` value of the `hapi-swagger` plugin options of the route.
 -   `uiCompleteScript`: (string || object) Called when UI loads. Can be javascript string injected into the HTML (ex: `'alert("I got you !")'`) or object with `src` property (ex: `{ src: '/assets/js/doc-patch.js' }`) that can point to reference remote file. The file will be loaded on demand and appended to DOM. Default is `null`.
 -   `validatorUrl`: (string || null) sets the external validating URL Can switch off by setting to `null`
+-   `tryItOutEnabled`: (boolean) controls whether the "Try it out" section should be enabled by default
 
 ## Plugin Specific Route Options
 
diff --git a/templates/index.html b/templates/index.html
index d205b67..9ecf4bf 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -77,7 +77,8 @@
           // patch: uiCompleteScript -- Rendering
           onComplete: function() {
             {{{ hapiSwagger.uiCompleteScript }}}
-          }
+          },
+          tryItOutEnabled: {{hapiSwagger.tryItOutEnabled}},
         }
 
         const ui = SwaggerUIBundle(swaggerOptions);


Test patch:
diff --git a/test/Integration/plugin-test.js b/test/Integration/plugin-test.js
index 49dda4c..b6544bb 100644
--- a/test/Integration/plugin-test.js
+++ b/test/Integration/plugin-test.js
@@ -461,6 +461,18 @@ lab.experiment('plugin', () => {
     expect(response.result.indexOf('swagger.json?tags=reduced') > -1).to.equal(true);
   });
 
+  lab.test('tryItOutEnabled true', async () => {
+    const server = await Helper.createServer({ tryItOutEnabled: true });
+    const response = await server.inject({ method: 'GET', url: '/swagger.json' });
+    expect(response.statusCode).to.equal(200);
+  });
+
+  lab.test('tryItOutEnabled false', async () => {
+    const server = await Helper.createServer({ tryItOutEnabled: false });
+    const response = await server.inject({ method: 'GET', url: '/swagger.json' });
+    expect(response.statusCode).to.equal(200);
+  });
+
   lab.test('test route x-meta appears in swagger', async () => {
     const testRoutes = [
       {