Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable "Try It Out" in Swagger

This question has been asked a lot for different languages. After a substantial amount of (fruitless) browsing Im feeling rather dumb but, I'll ask anyway

This document refers to adding a Swagger plugin in what appears to be Javascript https://github.com/swagger-api/swagger-ui/issues/3725

I tried adding that code as an injected Javascript resource:

c.InjectJavaScript(thisAssembly, "MyProject.Scripts.swagger-plugins.js");

Code in the .js file is

const DisableTryItOutPlugin = function() {
    return {
        statePlugins: {
            spec: {
                wrapSelectors: {
                    allowTryItOutFor: () => () => false
                }
            }
        }
    }
}

const DisableAuthorizePlugin = function () {
    return {
        wrapComponents: {
            AuthorizeBtn: () => () => null
        }
    }
}

SwaggerUI({
    plugins: [
        DisableTryItOutPlugin,
        DisableAuthorizePlugin
    ]
})

That achieved nothing and I have no idea where to go next.

like image 718
Jimbo Avatar asked Apr 30 '19 05:04

Jimbo


People also ask

How do I disable try it out in Swagger?

3.10. 0+. This config can also disable "Try it out" selectively for specific HTTP methods. For example, supportedSubmitMethods: ["get", "head"] keeps "Try it out" only for GET and HEAD, but disables it for POST, PUT, PATCH, DELETE and other methods.

What is @API annotation in Swagger?

Annotation Type Api. @Target(value=TYPE) @Retention(value=RUNTIME) @Inherited public @interface Api. Marks a class as a Swagger resource. By default, Swagger-Core will only include and introspect only classes that are annotated with @Api and will ignore other resources (JAX-RS endpoints, Servlets and so on).

Should I disable Swagger in production?

We should not enable swagger in production due to security threats. In.net core version 6.0 version, we can protect it with the below code in Program.

How do I hide schemas in Swagger?

To hide the "Models" section, add defaultModelsExpandDepth: -1 to the Swagger UI configuration code in your index. html . Note the option name uses plural Model*s* not Model . Swagger UI also has many other configuration options that control API documentation rendering.


2 Answers

you can hide Try it out button in swagger (using swashbuckle, C# dotnet core) -

Affected Code Line -

c.SupportedSubmitMethods(new Swashbuckle.AspNetCore.SwaggerUI.SubmitMethod[] { }

Entire code sample - (Add inside configure method)

 app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/test/swagger.json", "test API");
                c.SupportedSubmitMethods(new Swashbuckle.AspNetCore.SwaggerUI.SubmitMethod[] { });
            });

This is worked because, we have used SubmitMethod enum which have following values inside that -

    public enum SubmitMethod
    {
        Get = 0,
        Put = 1,
        Post = 2,
        Delete = 3,
        Options = 4,
        Head = 5,
        Patch = 6,
        Trace = 7
    }
like image 72
shrey Avatar answered Sep 20 '22 11:09

shrey


You can try with these lines in the Configure method in the startup.cs file

app.UseSwaggerUI(c =>
              {
                  if (!env.IsDevelopment())
                     c.SupportedSubmitMethods(new SubmitMethod[] { });

                  c.SwaggerEndpoint("/swagger/swagger.json", "API");
              });
like image 44
Juampi Ferrero Avatar answered Sep 17 '22 11:09

Juampi Ferrero