Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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));
}
}
Loading