servicestackopenapiswagger-ui

ServiceStack OpenAPI Swagger Exclude "auth" methods


How can I exclude these base "auth" methods circled in the screenshot from my ServiceStack application's Swagger docs? enter image description here


Solution

  • In OpenAPI v3 they can be removed with a Swashbuckle DocumentFilter you can register where you enable Swagger:

    services.AddSwaggerGen(c => {
        c.DocumentFilter<OpenApiDocumentFilter>();
    });
    

    Where the Document Filter removes all the paths you don't want:

    public class OpenApiDocumentFilter(OpenApiMetadata metadata) : IDocumentFilter
    {
        public void Apply(OpenApiDocument doc, DocumentFilterContext context)
        {
            doc.Paths.Remove("/auth");
            doc.Paths.Remove("/auth/{provider}");
            doc.Paths.Remove("/assignroles");
            doc.Paths.Remove("/unassignroles");
        }
    }
    

    In Open API v2 they can be removed with an ApiDeclarationFilter, e.g:

    Plugins.Add(new OpenApiFeature
    {
        ApiDeclarationFilter = doc => {
            doc.Paths.Remove("/auth");
            doc.Paths.Remove("/auth/{provider}");
            doc.Paths.Remove("/assignroles");
            doc.Paths.Remove("/unassignroles");
        }
    });