Skip to content

Commit b14513f

Browse files
committed
fix(symfony): fix property restrictions for root resource with dynamic validation groups
1 parent 0a1a1ea commit b14513f

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Symfony/Bundle/Resources/config/metadata/validator.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<argument type="service" id="validator" />
88
<argument type="service" id="api_platform.metadata.property.metadata_factory.validator.inner" />
99
<argument type="tagged_iterator" tag="api_platform.metadata.property_schema_restriction" />
10+
<argument type="tagged_locator" tag="api_platform.validation_groups_generator" />
1011
</service>
1112

1213
<service id="api_platform.metadata.property_schema.choice_restriction" class="ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaChoiceRestriction" public="false">

src/Symfony/Validator/Metadata/Property/ValidatorPropertyMetadataFactory.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use ApiPlatform\Metadata\ApiProperty;
1818
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
1919
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRestrictionMetadataInterface;
20+
use Psr\Container\ContainerInterface;
2021
use Symfony\Component\Validator\Constraint;
2122
use Symfony\Component\Validator\Constraints\Bic;
2223
use Symfony\Component\Validator\Constraints\CardScheme;
@@ -72,7 +73,7 @@ final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryI
7273
/**
7374
* @param PropertySchemaRestrictionMetadataInterface[] $restrictionsMetadata
7475
*/
75-
public function __construct(private readonly ValidatorMetadataFactoryInterface $validatorMetadataFactory, private readonly PropertyMetadataFactoryInterface $decorated, private readonly iterable $restrictionsMetadata = [])
76+
public function __construct(private readonly ValidatorMetadataFactoryInterface $validatorMetadataFactory, private readonly PropertyMetadataFactoryInterface $decorated, private readonly iterable $restrictionsMetadata = [], private readonly ?ContainerInterface $container = null)
7677
{
7778
}
7879

@@ -153,7 +154,18 @@ private function getValidationGroups(ValidatorClassMetadataInterface $classMetad
153154
{
154155
if (
155156
isset($options['validation_groups'])
156-
&& !\is_callable($options['validation_groups'])
157+
&& (
158+
!\is_callable($options['validation_groups'])
159+
&& !(
160+
1 === count($options['validation_groups'])
161+
&& null !== ($validationGroups = $options['validation_groups'][0] ?? null)
162+
&& $this->container
163+
&& \is_string($validationGroups)
164+
&& $this->container->has($validationGroups)
165+
&& ($service = $this->container->get($validationGroups))
166+
&& \is_callable($service)
167+
)
168+
)
157169
) {
158170
return $options['validation_groups'];
159171
}

0 commit comments

Comments
 (0)