Swashbuckle.webapi: Ability to ignore a specific route

Created on 11 Mar 2015  路  3Comments  路  Source: domaindrivendev/Swashbuckle.WebApi

Hello,

I was wondering if there is any easy way to ignore a specific route from the docs. I know that there is the [ApiExplorerSettings(IgnoreApi = true)] attribute but it doesn't work when my controller has multiple route attributes (and I want to ignore one of them)

c# [Route("~/api/v{version:int=1}/error")] [Route("~/api/v{version:int=2}/events")] [Route("~/api/v{version:int=2}/projects/{projectId:objectid}/events")]

Most helpful comment

Thanks, I was able to accomplish this with the following code:

    public class FilterRoutesDocumentFilter : IDocumentFilter {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer) {
            swaggerDoc.paths["/api/v{version}/error"].post = null;
        }
    }

All 3 comments

You could create an IDocumentFilter (see readme) that walks through the generated SwaggerDocument and remove certain PathItems (e.g. swaggerDoc.paths["/mypath"]) or individual operations within a path (e.g. swaggerDocs.paths["/mypath"].get = null).

Thanks, I was able to accomplish this with the following code:

    public class FilterRoutesDocumentFilter : IDocumentFilter {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer) {
            swaggerDoc.paths["/api/v{version}/error"].post = null;
        }
    }

I had to remove all routes starting with some prefix, so i simply queried all routes and remove them from swaggerDoc.paths collection. My solution is below

List<KeyValuePair<string, PathItem>> pathList = swaggerDoc.Paths.Where((x) => x.Key.StartsWith("/{prefix}")).ToList();

            // If paths are found, simply remove them from list
            if (pathList != null && pathList.Count > 0)
            {
                // Loop through all paths and remove it from our list
                pathList.ForEach(result => {
                    swaggerDoc.Paths.Remove(result.Key);
                });
            }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

qwertykeith picture qwertykeith  路  5Comments

guidoffm picture guidoffm  路  5Comments

maheshmohandas picture maheshmohandas  路  4Comments

raaga123 picture raaga123  路  4Comments

djem3000 picture djem3000  路  5Comments