How to hide services and endpoints in OpenAPI v3 swagger

Hi,

I’ve just upgraded my solution to 8.3 and the OpenApiFeature plugin to AddSwaggerGen, and some of my service-level decorations no longer work. What swagger class decorations must I use to hide a service and endpoint from swagger/index.html? I looked and looked and never found a list anywhere online.

My metadata page is fine (EndpointsApiExplorer seems to respect my old decorations).

Thanks,

Francis

You should still be able to use [ExcludeMetadata] attribute to hide APIs in Swagger UI, Metadata Pages, etc.

[ExcludeMetadata]
public class MyRequestDto { ... }

Actually that does not appear to work anymore:

I have these attibutes for the class:

[Tag(“Admin (Non-Public)”)]
[Restrict(VisibleLocalhostOnly = true, VisibleInternalOnly = true)]
[ServiceStack.DataAnnotations.ExcludeMetadata]
[ServiceStack.DataAnnotations.Exclude(Feature.ApiExplorer)]

The /metadata page hides all endpoints.

However, the /swagger/index.html page shows all of them.

My OpenAPI config:

[assembly: HostingStartup(typeof(myapp.ConfigureOpenApi))]

namespace myapp;

public class ConfigureOpenApi : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices((context, services) =>
{
services.AddEndpointsApiExplorer();

        services.AddSwaggerGen(options => options.SwaggerDoc("v1", new OpenApiInfo
        {
            Version = "v4",
            Title = "my API",
            Description = "An API to do whatever",
        }));

        services.AddServiceStackSwagger();
        services.AddTransient<IStartupFilter, StartupFilter>();
    });

public class StartupFilter : IStartupFilter
{
    public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => app =>
    {
        app.UseSwagger();
        app.UseSwaggerUI(options =>
        {
            options.SwaggerEndpoint("/swagger/swagger.json", "v4");
            options.RoutePrefix = string.Empty;
        });
        next(app);
    };
}

}

public class StartupFilter : IStartupFilter
{
    public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => app =>
    {
        app.UseSwagger();
        app.UseSwaggerUI(options =>
        {
            options.SwaggerEndpoint("/swagger/swagger.json", "v4");
            options.RoutePrefix = string.Empty;
        });
        next(app);
    };
}

}

You should only have one Exclude attribute, e.g. just having [ExcludeMetadata] should be all you need. I’ve tested it with the latest version and it’s working as expected.