Skip to content

MaxItems array validation not reflecting in swagger schema for query parameters  #132

@jleon15

Description

@jleon15

Hey everyone! We are using Fluent Validation for all of our API request models but those that contain maximum items validation on arrays in query parameters do not get the maxItems property set in the swagger schema.

Those arrays that are in the body and have the same validation applied do get reflected in the schema.

For example, for this request model:

public class GetTenantMembersModel : PaginatedQueryModel
{
    [FromQuery(Name = "user_id")]
    [JsonProperty("user_id")]
    public List<Guid> UserIds { get; set; } = new();

    ... 
}

We have the following validator

public class GetTenantMembersModelValidator : AbstractValidator<GetTenantMembersModel>
{
    public GetTenantMembersModelValidator()
    {
        RuleFor(x => x.UserIds)
            .MaximumItems(50)
            .WithMessage(ErrorMessages.HasMaximumItemsOfMaxItems)
            .ForPropertyName("user_id");

        ...
    }
}

And it results in the following swagger schema:

Screenshot 2023-05-26 at 12 39 10

As you can see, the maxItems property is not set, but for body parameters it is set using the exact same validation.

If I go ahead and use the [MaxLength(50)] ComponentModel annotation, it is added to the schema:

image

Is this a known issue? Is there any workaround? Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions