Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@smaglio81
Created September 10, 2016 03:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save smaglio81/e57a8bdf0541933d7004665a85a7b198 to your computer and use it in GitHub Desktop.
Save smaglio81/e57a8bdf0541933d7004665a85a7b198 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Web.Http.Description;
using Swashbuckle.Swagger;
namespace BoundedContext.Web.Swagger
{
public class LowercaseDocumentFilter : IDocumentFilter
{
public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
{
//////// PATHS
var paths = swaggerDoc.paths;
// generate the new keys
var newPaths = new Dictionary<string, PathItem>();
var removeKeys = new List<string>();
foreach (var path in paths)
{
var newKey = path.Key.ToLower();
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);
}
}
}
}
@mauriciogentile
Copy link

You should check your dictionary for repeated key before adding to avoid runtime issues. Other that that, the extension is great!

@eseneckiy
Copy link

One more case.
For instance, if we have an endpoint with parameter such as /api/v1/orders/{orderId} it changing to /api/v1/orders/{orderid}.
And when we try to make request instead of
curl -X GET "http://127.0.0.1:9001/api/v1/orders/1" -H "accept: application/json"
we receive
curl -X GET "http://127.0.0.1:9001/api/v1/orders/{orderid}" -H "accept: application/json"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment