Skip to content

Instantly share code, notes, and snippets.

@rafalkasa
Forked from smaglio81/LowercaseDocumentFilter.cs
Last active December 5, 2022 19:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rafalkasa/01d5e3b265e5aa075678e0adfd54e23f to your computer and use it in GitHub Desktop.
Save rafalkasa/01d5e3b265e5aa075678e0adfd54e23f to your computer and use it in GitHub Desktop.
Upgrade for Swashbuckle.AspNetCore 5.x
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
namespace BoundedContext.Web.Swagger
{
/// <summary>
/// This class orginally was created by https://gist.github.com/smaglio81 and modified version used in this project you can find here
/// https://gist.github.com/rafalkasa/01d5e3b265e5aa075678e0adfd54e23f
/// </summary>
public class LowercaseDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
//////// PATHS
var paths = swaggerDoc.Paths;
// generate the new keys
var newPaths = new Dictionary<string, OpenApiPathItem>();
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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment