Forked from smaglio81/LowercaseDocumentFilter.cs
Last active
December 5, 2022 19:02
-
-
Save rafalkasa/01d5e3b265e5aa075678e0adfd54e23f to your computer and use it in GitHub Desktop.
Upgrade for Swashbuckle.AspNetCore 5.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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