Krompaco

Removing Episerver Find from Swagger UI

I installed Swashbuckle in my Episerver site that of course also uses Find.

Firstly I got a crash that I got around by activating this line in my SwaggerConfig.cs (would be nice if Epi changed their methods to not conflict).

c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());

When I then browsed /swagger/ I got a bunch of Find methods listed. These operations are built using ASP.NET Web API and therefore gets listed by default. I only wanted my own API was in scope for this so I had to activate a custom filter in SwaggerConfig.

c.DocumentFilter<FindFilter>();

After trying out some different stuff this was the one crude way I found to exclude them.

private class FindFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {
        foreach (var path in swaggerDoc.paths)
        {
            string k = path.Key.ToLower();

            if (k.StartsWith("/episerver/find/api")
                || k.Contains("/api/bestbets")
                || k.Contains("/find_v2")
                || k.Contains("/api/click")
                || k.Contains("/api/editorialboosting"))
            {
                // Exclude from docs by nulling them
                var p = path.Value;
                p.delete = null;
                p.get = null;
                p.head = null;
                p.options = null;
                p.patch = null;
                p.post = null;
                p.put = null;
            }
        }
    }
}

Published and tagged with these categories: Episerver, ASP.NET, Development, Troubleshooting