-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b073e5d
commit e1ae84b
Showing
3 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace Syki.Back.Filters; | ||
|
||
public class HttpMethodSorterDocumentFilter : IDocumentFilter | ||
{ | ||
public void Apply(OpenApiDocument document, DocumentFilterContext context) | ||
{ | ||
// Order path groups (OpenApiPathItems) alphabetically | ||
var pathKvps = document.Paths | ||
.OrderBy(pathKvp => | ||
{ | ||
if (pathKvp.Value.Operations.Any(x => x.Key == OperationType.Post)) return 10; | ||
if (pathKvp.Value.Operations.Any(x => x.Key == OperationType.Get)) return 20; | ||
if (pathKvp.Value.Operations.Any(x => x.Key == OperationType.Put)) return 30; | ||
if (pathKvp.Value.Operations.Any(x => x.Key == OperationType.Delete)) return 40; | ||
return 50; | ||
}) | ||
.ToList(); | ||
|
||
document.Paths.Clear(); | ||
pathKvps.ForEach(kvp => document.Paths.Add(kvp.Key, kvp.Value)); | ||
|
||
// Order operations by method within each group | ||
document.Paths.ToList().ForEach(pathKvp => | ||
{ | ||
var operationKvps = pathKvp.Value.Operations | ||
.OrderBy(kvp => | ||
{ | ||
var weight = kvp.Key switch | ||
{ | ||
OperationType.Post => 10, | ||
OperationType.Get => 20, | ||
OperationType.Put => 30, | ||
OperationType.Delete => 40, | ||
_ => 50, | ||
}; | ||
return weight; | ||
}) | ||
.ToList(); | ||
|
||
pathKvp.Value.Operations.Clear(); | ||
operationKvps.ForEach(operationKvp => | ||
{ | ||
pathKvp.Value.Operations.Add(operationKvp); | ||
}); | ||
}); | ||
} | ||
} |