Skip to content

Instantly share code, notes, and snippets.

@hanssens
Forked from rafalkasa/LowercaseDocumentFilter.cs
Last active February 16, 2018 15:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanssens/3a0a398bbf0969afb40360a7e6371396 to your computer and use it in GitHub Desktop.
Save hanssens/3a0a398bbf0969afb40360a7e6371396 to your computer and use it in GitHub Desktop.
Updated to exclude parameters from being lowercased (thanks to @jonorogers)
using System.Collections.Generic;
using System.Linq;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Heartwork.Api.Application.Filters
{
public class LowercaseDocumentFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
// Lowercase all routes, for Swagger, as discussed here:
// https://github.com/domaindrivendev/Swashbuckle/issues/834
// Issue has a reference to the original gist, which can be found here:
// https://gist.github.com/smaglio81/e57a8bdf0541933d7004665a85a7b198
var originalPaths = swaggerDoc.Paths;
// generate the new keys
var newPaths = new Dictionary<string, PathItem>();
var removeKeys = new List<string>();
foreach (var path in originalPaths)
{
var newKey = LowercaseEverythingButParameters(path.Key);
if (newKey != path.Key)
{
removeKeys.Add(path.Key);
newPaths.Add(newKey, path.Value);
}
}
// add the new keys
foreach (var path in newPaths)
swaggerDoc.Paths.Add(path.Key, path.Value);
// remove the old keys
foreach (var key in removeKeys)
swaggerDoc.Paths.Remove(key);
}
private static string LowercaseEverythingButParameters(string key)
{
return string.Join('/', key.Split('/')
.Select(x => x.Contains("{")
? x
: x.ToLower()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment