diff --git a/src/Symfony/Tests/Validator/Metadata/Property/Restriction/PropertySchemaCountRestrictionTest.php b/src/Symfony/Tests/Validator/Metadata/Property/Restriction/PropertySchemaCountRestrictionTest.php index 8ab006b6996..4b760178886 100644 --- a/src/Symfony/Tests/Validator/Metadata/Property/Restriction/PropertySchemaCountRestrictionTest.php +++ b/src/Symfony/Tests/Validator/Metadata/Property/Restriction/PropertySchemaCountRestrictionTest.php @@ -18,6 +18,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; +use Symfony\Component\TypeInfo\Type; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\Count; use Symfony\Component\Validator\Constraints\Positive; @@ -59,5 +60,8 @@ public static function createProvider(): \Generator yield 'min items' => [new Count(min: 1), new ApiProperty(), ['minItems' => 1]]; yield 'max items' => [new Count(max: 10), new ApiProperty(), ['maxItems' => 10]]; yield 'min/max items' => [new Count(min: 1, max: 10), new ApiProperty(), ['minItems' => 1, 'maxItems' => 10]]; + yield 'list items' => [new Count(min: 1, max: 10), (new ApiProperty())->withNativeType(Type::list(Type::string())), ['minItems' => 1, 'maxItems' => 10]]; + yield 'string-keyed map properties' => [new Count(min: 1, max: 3), (new ApiProperty())->withNativeType(Type::array(Type::mixed(), Type::string())), ['minProperties' => 1, 'maxProperties' => 3]]; + yield 'nullable string-keyed map properties' => [new Count(max: 3), (new ApiProperty())->withNativeType(Type::nullable(Type::array(Type::mixed(), Type::string()))), ['maxProperties' => 3]]; } } diff --git a/src/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaCountRestriction.php b/src/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaCountRestriction.php index 55c3b5183fb..5c901775c1d 100644 --- a/src/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaCountRestriction.php +++ b/src/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaCountRestriction.php @@ -14,6 +14,11 @@ namespace ApiPlatform\Symfony\Validator\Metadata\Property\Restriction; use ApiPlatform\Metadata\ApiProperty; +use Symfony\Component\TypeInfo\Type; +use Symfony\Component\TypeInfo\Type\BuiltinType; +use Symfony\Component\TypeInfo\Type\CollectionType; +use Symfony\Component\TypeInfo\Type\NullableType; +use Symfony\Component\TypeInfo\TypeIdentifier; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\Count; @@ -30,13 +35,14 @@ class PropertySchemaCountRestriction implements PropertySchemaRestrictionMetadat public function create(Constraint $constraint, ApiProperty $propertyMetadata): array { $restriction = []; + $isMap = $this->isStringKeyedMap($propertyMetadata); if (null !== $constraint->min) { - $restriction['minItems'] = $constraint->min; + $restriction[$isMap ? 'minProperties' : 'minItems'] = $constraint->min; } if (null !== $constraint->max) { - $restriction['maxItems'] = $constraint->max; + $restriction[$isMap ? 'maxProperties' : 'maxItems'] = $constraint->max; } return $restriction; @@ -49,4 +55,16 @@ public function supports(Constraint $constraint, ApiProperty $propertyMetadata): { return $constraint instanceof Count; } + + private function isStringKeyedMap(ApiProperty $propertyMetadata): bool + { + $type = $propertyMetadata->getNativeType(); + + if ($type instanceof NullableType) { + $type = $type->getWrappedType(); + } + + return $type instanceof CollectionType + && !$type->getCollectionKeyType()->isSatisfiedBy(static fn (Type $t): bool => $t instanceof BuiltinType && $t->isIdentifiedBy(TypeIdentifier::INT)); + } }