Do not render Try It Out button and enable Execute button in Swagger UI

Viewed 654

Since it was first added I have been frustrated by the extra click required by the "Try it out" button. It slows down testing and does not seem to serve a purpose. (Feel free to enlighten me if I am wrong.) I would like an option to simply not render it and have the Execute button automatically enabled when I expand the endpoint.

To be clear, I do not want to disable it, I want it gone, as in the "old days".

As a stop gap I implemented some JavaScript that monitors the screen and, when an endpoint is expanded, it "clicks" the try it out button and then hides it. This works but is not ideal because the button briefly appears then is removed. (i.e an ugly hack and it is constantly "running" in the background)

2 Answers

If you use swagger-ui-express, you can enable the tryItOutEnabled in your Swagger UI options like so:

Main application file:

const app = express();

//more code

//Swagger integration
const swaggerUi = require('swagger-ui-express');

//Swagger Document
const swaggerDocument = require('./swagger.json');

//Swagger options
const swaggerOptions = {
    explorer: true,
    customCss: '.swagger-ui textarea { min-height:60px }',
    swaggerOptions: {
        persistAuthorization: true,
        tryItOutEnabled: true //<---- enable "try it out enabled" here
    }
};

//Assign Swagger route
app.use(`${config.API_PREFIX}/swagger`, swaggerUi.serve,  swaggerUi.setup(swaggerDocument, swaggerOptions));

Source: https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md#user-content-tryitoutenabled

Configure your Swagger UI with the tryItOutEnabled: true option (it was added in v. 3.41.0). For example, if you serve the dist assets directly:

// This code is either in `swagger-initializer.js` or `index.html`,
// depending on your Swagger UI version

const ui = SwaggerUIBundle({
  dom_id: "#swagger-ui",
  url: "https://petstore.swagger.io/v2/swagger.json",

  tryItOutEnabled: true,  // <-------
  ...
})

Demo: https://petstore.swagger.io/?tryItOutEnabled=true

If your Swagger UI is bundled with some library, such as Swashbuckle or Springfox, check if this library provides a config option to set Swagger UI's tryItOutEnabled option.

Related