如何使用代码swashbuckle的数组类型设置查询参数的默认值
我目前正在使用.NET Core 3.1和Swashbuckle库。我已经用属性和适当的招摇描述的请求制作了几个端点(我想继续这样做)。
现在,我的类型为数组的查询参数有问题。 用要添加的项目显示此属性是没有问题的。 在此处输入图像描述 我希望用默认的12个值来实现它们。可能是12 x 0。 类似:
[DefaultValue(new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })]
我还设置了[maxlength(12)],但它也不起作用。
我现在看到的是数字的验证都无法使用。
就目前而言,看起来像这样:
/// <summary>
/// MyArray with 12x0 values.
/// </summary>
[DefaultValue(new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })]
[MaxLength(12)]
[Range(0, 1)]
public int[] MyArray { get; set; }
我想实现什么?
- 12个项目的默认值,0。
- 不少于12个项目。
- 验证范围为0,1,
谢谢!
编辑: 我还尝试了自定义属性filter,但它在Swagger UI上没有任何变化...
public class QueryArrayParameterFilter : IParameterFilter
{
public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
{
if (!parameter.In.HasValue || parameter.In.Value != ParameterLocation.Query)
return;
if (parameter.Schema?.Type == "array" && parameter.Name.Equals("MyArray"))
{
var value = null as IOpenApiExtension;
parameter.Extensions.TryGetValue("explode", out value);
if (value == null)
{
parameter.Extensions.Add("explode", new OpenApiBoolean(false));
}
parameter.Schema = new OpenApiSchema
{
Type = "array",
Items = new OpenApiSchema()
{
MaxItems = 2,
Type = "integer",
Format = "int64",
Example = new OpenApiArray()
{
new OpenApiInteger(0),
new OpenApiInteger(0)
},
Default = new OpenApiArray()
{
new OpenApiInteger(0),
new OpenApiInteger(0)
}
},
MaxItems = 3
};
}
}
}
I am currently working with .NET Core 3.1 and Swashbuckle library. I have made several endpoints with requests decorated with attributes and proper swagger descriptions (I want to continue doing it this way).
Now I have a problem with the query parameter whose type is Array.
It is no problem to show this property with items to add.
enter image description here
I wish to fulfill them with default 12 values. It could be 12 x 0.
Something like:
[DefaultValue(new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })]
Also, I set [MaxLength(12)], but it also does not work.
I see for now even validation for numbers does not work.
For now, it looks like this:
/// <summary>
/// MyArray with 12x0 values.
/// </summary>
[DefaultValue(new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })]
[MaxLength(12)]
[Range(0, 1)]
public int[] MyArray { get; set; }
What do I want to achieve?
- default values for 12 items with 0.
- no less and no more than 12 items.
- validation is in range 0,1
Thank you in advance!
Edit:
I have also tried custom AttributeFilter, but it changes nothing on swagger UI...
public class QueryArrayParameterFilter : IParameterFilter
{
public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
{
if (!parameter.In.HasValue || parameter.In.Value != ParameterLocation.Query)
return;
if (parameter.Schema?.Type == "array" && parameter.Name.Equals("MyArray"))
{
var value = null as IOpenApiExtension;
parameter.Extensions.TryGetValue("explode", out value);
if (value == null)
{
parameter.Extensions.Add("explode", new OpenApiBoolean(false));
}
parameter.Schema = new OpenApiSchema
{
Type = "array",
Items = new OpenApiSchema()
{
MaxItems = 2,
Type = "integer",
Format = "int64",
Example = new OpenApiArray()
{
new OpenApiInteger(0),
new OpenApiInteger(0)
},
Default = new OpenApiArray()
{
new OpenApiInteger(0),
new OpenApiInteger(0)
}
},
MaxItems = 3
};
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论