【问题标题】:Swagger Parameter Filter in ASP.NET Web ApiASP.NET Web Api 中的 Swagger 参数过滤器
【发布时间】:2021-10-07 18:50:55
【问题描述】:

我有 ASP.NET Core 应用程序,我可以在其中扩展 swagger 枚举,

public class MyParameterFilter : IParameterFilter
{
    /// <inheritdoc />
    public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
    {
        var routeInfo = context.ApiParameterDescription.RouteInfo;
        if (routeInfo?.Constraints != null && routeInfo.Constraints.Any(c => c is MyConstraint))
        {
            parameter.Schema.Enum = Myvalues.Select(p => new OpenApiString(p)).ToList<IOpenApiAny>();
        }
    }
}

现在我想在我的经典 ASP.NET Web Api 项目中做同样的事情,我看到 DocumentFilter、OperationFilter 和 SchemaFilter 但没有 ParameterFilter。我的意思是我找不到 IParameterFilter

【问题讨论】:

    标签: c# asp.net asp.net-web-api swagger swashbuckle


    【解决方案1】:

    使用过的文档过滤器,

    public class MyDocumentFilter : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
        {
            var pathItems = swaggerDoc.paths.Values;
            
            var deletes = pathItems.Select(pathItem => pathItem.delete).Where(o => o?.parameters != null);
            var gets = pathItems.Select(pathItem => pathItem.get).Where(o => o?.parameters != null);
            var heads = pathItems.Select(pathItem => pathItem.head).Where(o => o?.parameters != null);
            var patches = pathItems.Select(pathItem => pathItem.patch).Where(o => o?.parameters != null);
            var puts = pathItems.Select(pathItem => pathItem.put).Where(o => o?.parameters != null);
            var posts = pathItems.Select(pathItem => pathItem.post).Where(o => o?.parameters != null);
            var options = pathItems.Select(pathItem => pathItem.options).Where(o => o?.parameters != null);
            
            var allOperations = deletes.Concat(gets)
                                       .Concat(heads)
                                       .Concat(patches)
                                       .Concat(puts)
                                       .Concat(posts)
                                       .Concat(options)
                                       .ToList();
    
            foreach (Operation operation in allOperations.Where(o => o.parameters.Any(p => p.name == "propName")))
            {
                operation.parameters.First(p => p.name == "propName").@enum = MyValues.Select(p => (object)p.Name).ToList();
                var successResponse = operation.responses.First(d => d.Key == "200").Value;
                if (successResponse == null)
                {
                    return;
                }
                successResponse.examples = MyValues.ToDictionary(d => d.Name, d => "string");
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 2013-04-09
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 2018-03-27
      • 1970-01-01
      相关资源
      最近更新 更多