Skip to content

[Bug]: MinLength attribute on a dictionary property generates invalid minLength constraint #3915

@spanglerco

Description

@spanglerco

Describe the bug

I have a model class with a dictionary property. At request time, I want to require the dictionary be non-empty. MVC supports doing that via the [MinLength] attribute:

[Required]
[MinLength(1)]
public IReadOnlyDictionary<string, string?> Values { get; }

However, Swashbuckle then applies "minLength": 1 to the schema definition instead of "minProperties": 1.

Expected behavior

The generated OpenAPI document includes "minProperties": 1 for the Values schema.

Actual behavior

The generated OpenAPI document includes "minLength": 1 for the Values schema.

Steps to reproduce

public class MyModel
{
    [Required]
    [MinLength(1)]
    public IReadOnlyDictionary<string, string?> Values { get; init; }
}

[ApiController]
public class MyController
{
    [HttpPost]
    public void MyAction([FromBody] MyModel model)
    {
    }
}

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

10.1.7

.NET Version

10.0.202

Anything else?

This was found by running the vacuum linter against the generated OpenAPI document.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp-wantedA change up for grabs for contributions from the community

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions