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 @@ -17,8 +17,6 @@
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRestrictionMetadataInterface;
use ApiPlatform\Symfony\Validator\ValidationGroupsExtractorTrait;
use Psr\Container\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Bic;
use Symfony\Component\Validator\Constraints\CardScheme;
Expand All @@ -28,6 +26,7 @@
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Iban;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\Isbn;
Expand All @@ -49,10 +48,6 @@
*/
final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryInterface
{
use ValidationGroupsExtractorTrait {
getValidationGroups as extractValidationGroups;
}

/**
* @var string[] A list of constraint classes making the entity required
*/
Expand All @@ -78,13 +73,8 @@ final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryI
/**
* @param PropertySchemaRestrictionMetadataInterface[] $restrictionsMetadata
*/
public function __construct(
private readonly ValidatorMetadataFactoryInterface $validatorMetadataFactory,
private readonly PropertyMetadataFactoryInterface $decorated,
private readonly iterable $restrictionsMetadata = [],
?ContainerInterface $container = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bc but as it was tagged like a week ago and we revert it impacts should be negligible.

) {
$this->container = $container;
public function __construct(private readonly ValidatorMetadataFactoryInterface $validatorMetadataFactory, private readonly PropertyMetadataFactoryInterface $decorated, private readonly iterable $restrictionsMetadata = [])
{
}

/**
Expand Down Expand Up @@ -162,8 +152,14 @@ public function create(string $resourceClass, string $property, array $options =
*/
private function getValidationGroups(ValidatorClassMetadataInterface $classMetadata, array $options): array
{
if (null !== ($groups = $this->extractValidationGroups($options['validation_groups'] ?? null))) {
return $groups;
if (isset($options['validation_groups'])) {
if ($options['validation_groups'] instanceof GroupSequence) {
return $options['validation_groups']->groups;
}

if (!\is_callable($options['validation_groups'])) {
return $options['validation_groups'];
}
}

if (!method_exists($classMetadata, 'getDefaultGroup')) {
Expand Down
50 changes: 0 additions & 50 deletions src/Symfony/Validator/ValidationGroupsExtractorTrait.php

This file was deleted.

26 changes: 21 additions & 5 deletions src/Symfony/Validator/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Validator\Exception\ValidationException;
use ApiPlatform\Validator\ValidatorInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidatorInterface;

/**
Expand All @@ -25,19 +26,34 @@
*/
final class Validator implements ValidatorInterface
{
use ValidationGroupsExtractorTrait;

public function __construct(private readonly SymfonyValidatorInterface $validator, ?ContainerInterface $container = null)
public function __construct(private readonly SymfonyValidatorInterface $validator, private readonly ?ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function validate(object $data, array $context = []): void
{
$violations = $this->validator->validate($data, null, $this->getValidationGroups($context['groups'] ?? null, $data));
if (null !== $validationGroups = $context['groups'] ?? null) {
if (
$this->container
&& \is_string($validationGroups)
&& $this->container->has($validationGroups)
&& ($service = $this->container->get($validationGroups))
&& \is_callable($service)
) {
$validationGroups = $service($data);
} elseif (\is_callable($validationGroups)) {
$validationGroups = $validationGroups($data);
}

if (!$validationGroups instanceof GroupSequence) {
$validationGroups = (array) $validationGroups;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But passing array of groups is not the same as passing GroupSequence 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a revert of changes made in #7184

This condition says - if $validationGroups is not an instance of GroupSequence, make it an array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right,read it wrong 🤦‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, yes ) Yesterday I did the same ))

}
}

$violations = $this->validator->validate($data, null, $validationGroups);
if (0 !== \count($violations)) {
throw new ValidationException($violations);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Symfony/Validator/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Container\ContainerInterface;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface;
Expand Down Expand Up @@ -119,4 +120,22 @@ public function testValidatorWithScalarGroup(): void
$validator = new Validator($symfonyValidator, $containerProphecy->reveal());
$validator->validate(new DummyEntity(), ['groups' => 'foo']);
}

public function testValidatorWithGroupSequence(): void
{
$data = new DummyEntity();
$expectedValidationGroups = new GroupSequence(['foo', 'bar']);

$constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationListProphecy->count()->willReturn(0);

$symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
$symfonyValidatorProphecy->validate($data, null, $expectedValidationGroups)->willreturn($constraintViolationListProphecy->reveal())->shouldBeCalled();
$symfonyValidator = $symfonyValidatorProphecy->reveal();

$containerProphecy = $this->prophesize(ContainerInterface::class);

$validator = new Validator($symfonyValidator, $containerProphecy->reveal());
$validator->validate(new DummyEntity(), ['groups' => $expectedValidationGroups]);
}
}
Loading