ste*_*vec 5 c# swagger swashbuckle asp.net-core
我正在编写一个实现Swashbuckle.AspNetCore.SwaggerGen.IDocumentFilter
,它在我的 swagger 配置文件的路径级别添加 x-summary 值。为此,它需要访问每个 Web 方法的以下两条信息
看来我可以从提供给 IDocumentFilter 实现的 #1context
和 #2 中获取swaggerDoc
,但除了使用路径之外,我找不到链接它们的好方法。
有更简洁的方法吗?
下面是我正在做的事情的一个简化示例。
public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
{
// Create a map from path (prepended with "/") to the custom attribute
var methodsByPath = context.ApiDescriptions
.ToDictionary(
m => $"/{m.RelativePath}",
m => ((ControllerActionDescriptor)m.ActionDescriptor).MethodInfo.GetCustomAttribute<MyCustomAttribute>());
// Add x-summary to each path
foreach (var pathItem in swaggerDoc.Paths)
{
var customAttribute = methodsByPath[pathItem.Key];
pathItem.Value.Extensions["x-summary"]
= GeneratePathDescription(pathItem.Value.Post.Summary, customAttribute);
}
}
string GeneratePathDescription(string methodSummary, MyCustomAttribute attr)
{
[snip]
}
Run Code Online (Sandbox Code Playgroud)
您的实现对我来说看起来非常简洁,但是如果您正在寻找如何“最好”实现 IDocumentFilter 的示例,请查看 Swashbuckle 的代码:
归档时间: |
|
查看次数: |
9953 次 |
最近记录: |