From 29c4eb76eb9ec0891d39f74f4b8263b471c106ec Mon Sep 17 00:00:00 2001 From: c1tru55 Date: Thu, 29 May 2025 15:52:55 +0600 Subject: [PATCH] fix(symfony): fix property restrictions for root resource with dynamic validation groups --- .../Resources/config/metadata/validator.xml | 1 + .../ValidatorPropertyMetadataFactory.php | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/Resources/config/metadata/validator.xml b/src/Symfony/Bundle/Resources/config/metadata/validator.xml index 1f50b6fe007..f47d2045f01 100644 --- a/src/Symfony/Bundle/Resources/config/metadata/validator.xml +++ b/src/Symfony/Bundle/Resources/config/metadata/validator.xml @@ -7,6 +7,7 @@ + diff --git a/src/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php b/src/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php index 4b27fa11f3a..1352bca5e45 100644 --- a/src/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php +++ b/src/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php @@ -17,6 +17,7 @@ use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface; use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRestrictionMetadataInterface; +use Psr\Container\ContainerInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\Constraints\Bic; use Symfony\Component\Validator\Constraints\CardScheme; @@ -72,7 +73,7 @@ final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryI /** * @param PropertySchemaRestrictionMetadataInterface[] $restrictionsMetadata */ - public function __construct(private readonly ValidatorMetadataFactoryInterface $validatorMetadataFactory, private readonly PropertyMetadataFactoryInterface $decorated, private readonly iterable $restrictionsMetadata = []) + public function __construct(private readonly ValidatorMetadataFactoryInterface $validatorMetadataFactory, private readonly PropertyMetadataFactoryInterface $decorated, private readonly iterable $restrictionsMetadata = [], private readonly ?ContainerInterface $container = null) { } @@ -153,7 +154,18 @@ private function getValidationGroups(ValidatorClassMetadataInterface $classMetad { if ( isset($options['validation_groups']) - && !\is_callable($options['validation_groups']) + && ( + !\is_callable($options['validation_groups']) + && !( + 1 === \count($options['validation_groups']) + && null !== ($validationGroups = $options['validation_groups'][0] ?? null) + && $this->container + && \is_string($validationGroups) + && $this->container->has($validationGroups) + && ($service = $this->container->get($validationGroups)) + && \is_callable($service) + ) + ) ) { return $options['validation_groups']; }