C# 将查询字符串参数添加到我的招摇过市规格
我在Web API中使用了Swashback(C#的swagger)。我有几个返回列表的GET端点,我允许用户在QueryString中添加perpage和page参数 例如: 我看到swagger确实支持“query”中的参数,但如何让Swashback这样做呢C# 将查询字符串参数添加到我的招摇过市规格,c#,swagger,swashbuckle,C#,Swagger,Swashbuckle,我在Web API中使用了Swashback(C#的swagger)。我有几个返回列表的GET端点,我允许用户在QueryString中添加perpage和page参数 例如: 我看到swagger确实支持“query”中的参数,但如何让Swashback这样做呢 我在一篇评论中提到,我通过创建一个自定义属性来解决我的问题,允许我做我需要的事情。以下是我的解决方案的代码: [AttributeUsage(AttributeTargets.Method, Inherited = false, A
我在一篇评论中提到,我通过创建一个自定义属性来解决我的问题,允许我做我需要的事情。以下是我的解决方案的代码:
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public class SwaggerParameterAttribute : Attribute
{
public SwaggerParameterAttribute(string name, string description)
{
Name = name;
Description = description;
}
public string Name { get; private set; }
public Type DataType { get; set; }
public string ParameterType { get; set; }
public string Description { get; private set; }
public bool Required { get; set; } = false;
}
在Swagger配置中注册该属性:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.OperationFilter<SwaggerParametersAttributeHandler>();
});
你可以很容易地做到这一点。假设您有一个具有如下操作的
ItemsController
:
[Route("/api/items/{id}")]
public IHttpActionResult Get(int id, int? page = null, int? perpage = null)
{
// some relevant code
return Ok();
}
[SwaggerParameter("$top", "Odata Top parameter", DataType = "integer", ParameterType ="query")]
Swashback将生成此规范(仅显示相关零件):
当您需要
page
和perpage
时,只需使参数不可为空。这里有一些关于Swagger ParametersAttributeHandler缺少信息的注释。它是一个操作过滤器,可帮助您确定对属性执行的操作
下面是我使用的一个示例处理程序,它允许我使用SwaggerParameterAttribute覆盖可空参数的必填字段
public class RequiredParameterOverrideOperationFilter : IOperationFilter
{
public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
{
// Get all SwaggerParameterAttributes on the method
var attributes = apiDescription.ActionDescriptor.GetCustomAttributes<SwaggerParameterAttribute>();
if (operation.parameters == null)
{
operation.parameters = new List<Parameter>();
}
// For each attribute found, find the operation parameter (this is where Swagger looks to generate the Swagger doc)
// Override the required fields based on the attribute's required field
foreach (var attribute in attributes)
{
var referencingOperationParameter = operation.parameters.FirstOrDefault(p => p.name == attribute.Name);
if (referencingOperationParameter != null)
{
referencingOperationParameter.required = attribute.Required;
}
}
}
}
公共类所需参数OverrideOperationFilter:IOperationFilter
{
public void Apply(操作,SchemaRegistry SchemaRegistry,apisdescription apisdescription)
{
//获取该方法的所有属性
var attributes=apiscription.ActionDescriptor.GetCustomAttributes();
if(operation.parameters==null)
{
operation.parameters=新列表();
}
//对于找到的每个属性,找到操作参数(这是Swagger生成Swagger文档的地方)
//基于属性的必填字段重写必填字段
foreach(属性中的var属性)
{
var referencengOperationParameter=operation.parameters.FirstOrDefault(p=>p.name==attribute.name);
if(referencengOperationParameter!=null)
{
ReferenceGoOperationParameter.required=属性.required;
}
}
}
}
以下是属性方法所需步骤的摘要(ASP.Net Core 2.1,Swashback.AspNetCore v4.0.1)。我需要一个以“$”开头的参数,所以可选参数不是选项
招摇过市
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public class SwaggerParameterAttribute : Attribute
{
public SwaggerParameterAttribute(string name, string description)
{
Name = name;
Description = description;
}
public string Name { get; private set; }
public string DataType { get; set; }
public string ParameterType { get; set; }
public string Description { get; private set; }
public bool Required { get; set; } = false;
}
斯威格参数:Tributefilter.cs
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Linq;
public class SwaggerParameterAttributeFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
var attributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
.Union(context.MethodInfo.GetCustomAttributes(true))
.OfType<SwaggerParameterAttribute>();
foreach (var attribute in attributes)
operation.Parameters.Add(new NonBodyParameter
{
Name = attribute.Name,
Description = attribute.Description,
In = attribute.ParameterType,
Required = attribute.Required,
Type = attribute.DataType
});
}
}
数据类型可以是:整数、字符串、布尔值
ParameterTypes:可以是path、body、query这是一个非常合理的答案,但我最终创建了一个自定义的Swagger属性来处理它@JasonH是如何实现的?是否有在线来源可以指向您提到的解决方案?我正在处理的代码不属于我自己,因此我没有共享我创建的属性。我可以用我所做的来编辑我的原始问题,这样你就可以看到了。谢谢@JasonH。你所举的例子有什么遗漏吗?斯威格怎么知道如何解析你创建的这个属性?对不起,你是对的,我忘了一个重要的部分。您必须将该属性注册到Swagger配置中。我编辑了我的原始帖子来展示这一点。
SwaggerParametersAttributeHandler
来自哪里显然,参数类型
枚举也丢失了。你有没有可能帮我们填一下<代码>:D
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Linq;
public class SwaggerParameterAttributeFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
var attributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
.Union(context.MethodInfo.GetCustomAttributes(true))
.OfType<SwaggerParameterAttribute>();
foreach (var attribute in attributes)
operation.Parameters.Add(new NonBodyParameter
{
Name = attribute.Name,
Description = attribute.Description,
In = attribute.ParameterType,
Required = attribute.Required,
Type = attribute.DataType
});
}
}
using Swashbuckle.AspNetCore.Swagger;
services.AddSwaggerGen(c =>
{
c.OperationFilter<SwaggerParameterAttributeFilter>();
c.SwaggerDoc("v1.0", new Info { Title = "My API", Version = "v1.0" });
});
[SwaggerParameter("$top", "Odata Top parameter", DataType = "integer", ParameterType ="query")]