Skip to content

fix(symfony): fix property restrictions for root resource with dynamic validation groups #7184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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,6 +17,8 @@
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 Down Expand Up @@ -47,6 +49,10 @@
*/
final class ValidatorPropertyMetadataFactory implements PropertyMetadataFactoryInterface
{
use ValidationGroupsExtractorTrait {
getValidationGroups as extractValidationGroups;
}

/**
* @var string[] A list of constraint classes making the entity required
*/
Expand All @@ -72,8 +78,13 @@ 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 = [],
?ContainerInterface $container = null,
) {
$this->container = $container;
}

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

if (!method_exists($classMetadata, 'getDefaultGroup')) {
Expand Down
50 changes: 50 additions & 0 deletions src/Symfony/Validator/ValidationGroupsExtractorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Symfony\Validator;

use Psr\Container\ContainerInterface;
use Symfony\Component\Validator\Constraints\GroupSequence;

trait ValidationGroupsExtractorTrait
{
/**
* A service locator for ValidationGroupsGenerator.
*/
private ?ContainerInterface $container = null;

public function getValidationGroups(\Closure|array|string|null $validationGroups, ?object $data = null): string|array|GroupSequence|null
{
if (null === $validationGroups) {
return $validationGroups;
}

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;
}

return $validationGroups;
}
}
26 changes: 5 additions & 21 deletions src/Symfony/Validator/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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 @@ -26,34 +25,19 @@
*/
final class Validator implements ValidatorInterface
{
public function __construct(private readonly SymfonyValidatorInterface $validator, private readonly ?ContainerInterface $container = null)
use ValidationGroupsExtractorTrait;

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

/**
* {@inheritdoc}
*/
public function validate(object $data, array $context = []): void
{
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;
}
}

$violations = $this->validator->validate($data, null, $validationGroups);
$violations = $this->validator->validate($data, null, $this->getValidationGroups($context['groups'] ?? null, $data));
if (0 !== \count($violations)) {
throw new ValidationException($violations);
}
Expand Down
Loading