如何使用代码swashbuckle的数组类型设置查询参数的默认值

发布于 2025-01-24 16:41:22 字数 2185 浏览 2 评论 0原文

我目前正在使用.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文