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")]
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);
});
}
Most helpful comment
Thanks, I was able to accomplish this with the following code: