C# Swashbuckle IDocumentFilter实现-如何将ActionDescriptor.MethodInfo链接到操作 项目:ASP Net核心2.2,Web API 包装:Swashback.AspNetCore(4.0.1)

C# Swashbuckle IDocumentFilter实现-如何将ActionDescriptor.MethodInfo链接到操作 项目:ASP Net核心2.2,Web API 包装:Swashback.AspNetCore(4.0.1),c#,asp.net-core,swagger,swashbuckle,C#,Asp.net Core,Swagger,Swashbuckle,我正在编写一个实现swashback.AspNetCore.swagggegen.IDocumentFilter,它在我的swagger配置文件中的路径级别添加x-summary值。为此,它需要访问每个web方法的以下两条信息 ApiDescription.ActionDescriptor.MethodInfo-获取对方法属性的访问权限 Operation.Summary-方法的Xml注释 似乎我可以从提供给IDocumentFilter实现的上下文中获取#1,从swaggerDoc中获取#2,

我正在编写一个实现
swashback.AspNetCore.swagggegen.IDocumentFilter
,它在我的swagger配置文件中的路径级别添加x-summary值。为此,它需要访问每个web方法的以下两条信息

  • ApiDescription.ActionDescriptor.MethodInfo-获取对方法属性的访问权限
  • Operation.Summary-方法的Xml注释
  • 似乎我可以从提供给IDocumentFilter实现的
    上下文中获取#1,从
    swaggerDoc中获取#2,但除了使用路径之外,我找不到链接它们的好方法

    有没有更整洁的方法

    下面是我正在做的一个简单例子

    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]
    }
    
    public void应用(swagger文档swagger文档、文档过滤器上下文)
    {
    //创建从路径(以“/”开头)到自定义属性的映射
    var methodsByPath=context.apiscriptions
    .ToDictionary(
    m=>$“/{m.RelativePath}”,
    m=>((ControllerActionDescriptor)m.ActionDescriptor.MethodInfo.GetCustomAttribute());
    //将x摘要添加到每个路径
    foreach(swaggerDoc.path中的var pathItem)
    {
    var customAttribute=methodsByPath[pathItem.Key];
    pathItem.Value.Extensions[“x-summary”]
    =GeneratePathDescription(pathItem.Value.Post.Summary,customAttribute);
    }
    }
    string GeneratePathDescription(string方法摘要,MyCustomAttribute属性属性)
    {
    [剪报]
    }
    
    我觉得您的实现非常简洁,但如果您想在Swashback的代码中寻找如何“最好”实现IDocumentFilter的示例,请参见: