How to omit methods from Swagger documentation on WebAPI using Swashbuckle

前端 未结 10 797
不思量自难忘°
不思量自难忘° 2020-11-30 19:38

I have a C# ASP.NET WebAPI application with API documentation being automatically generated using Swashbuckle. I want to be able to omit certain methods fr

相关标签:
10条回答
  • 2020-11-30 20:30

    You can remove "operations" from the swagger document after it's generated with a document filter - just set the verb to null (though, there may be other ways to do it as well)

    The following sample allows only GET verbs - and is taken from this issue.

    class RemoveVerbsFilter : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            foreach (PathItem path in swaggerDoc.paths.Values)
            {
                path.delete = null;
                //path.get = null; // leaving GET in
                path.head = null;
                path.options = null;
                path.patch = null;
                path.post = null;
                path.put = null;
            }
        }
    }
    

    and in your swagger config:

    ...EnableSwagger(conf => 
    {
        // ...
    
        conf.DocumentFilter<RemoveVerbsFilter>();
    });
    
    0 讨论(0)
  • 2020-11-30 20:33

    You can add the following attribute to Controllers and Actions to exclude them from the generated documentation: [ApiExplorerSettings(IgnoreApi = true)]

    0 讨论(0)
  • 2020-11-30 20:34

    I would prefer to remove the dictionary entries for path items completely:

    var pathsToRemove = swaggerDoc.Paths
                    .Where(pathItem => !pathItem.Key.Contains("api/"))
                    .ToList();
    
    foreach (var item in pathsToRemove)
    {
        swaggerDoc.Paths.Remove(item.Key);
    }
    

    With this approach, you would not get "empty" items in the generated swagger.json definition.

    0 讨论(0)
  • 2020-11-30 20:39

    If you want to prevent to show in NestJS the use

     @ApiExcludeEndpoint(true)
    
    0 讨论(0)
提交回复
热议问题