Skip to content

[Bug]: Symfony Constraints Count should set (min|max)Properties instead of (min|max)Items on maps (array<X, Y>) #2578

@gnutix

Description

@gnutix

Version

5.6.5

Description

Let's say you have a property like this one :

    /**
     * @var array<string, mixed>
     */
    #[Assert\NotNull, Assert\Count(min: 1)]
    public array $someMap;

The generated OpenApi will currently be :

{
    "someMap": {
        "type": "object",
        "minItems:" 1
    }
}

But "object" doesn't have a "minItems" (nor max) property in OpenAPI, only "minProperties" (and max). So currently I've gone around it in our custom describer using this hack :

if ($this->isArrayMap($property)) {
    if (Generator::UNDEFINED !== $property->minItems) {
        $property->minProperties = $property->minItems;
        $property->minItems = Generator::UNDEFINED /** @phpstan-ignore assign.propertyType */;
    }
    if (Generator::UNDEFINED !== $property->maxItems) {
        $property->maxProperties = $property->maxItems;
        $property->maxItems = Generator::UNDEFINED /** @phpstan-ignore assign.propertyType */;
    }
}

Which also reveals that "(min|max)Items" typing is wrong (it says @var int but is initialized with Generator::UNDEFINED).

The fix should most probably be in SymfonyConstraintAnnotationReader lines 90-96.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions