Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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)
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.
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
This can be done in the .net Startup
class or in Program.cs
in later versions of .net by:
if (app.Environment.IsDevelopment())
app.UseSwagger();
app.UseSwaggerUI(options => options.EnableTryItOutByDefault()); //options.SupportedSubmitMethods(new SubmitMethod[1] {SubmitMethod.Get}));
Obviously, it's just line 3 that does the trick. The commented bit is in case you want to enable or disable 'Try It Out' by http verb.
app
is the WebApplication
object you get from WebApplication.CreateBuilder(args).Build()
I've got to say, EnableTryItOutByDefault
seemed like the opposite of what I needed initially.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.