Skip to content

Commit 629da78

Browse files
authored
fix(symfony): use non deprecated validator exception (#6297)
* fix(symfony): use non deprecated validator exception * validation excetpion * fix constraint violation list aware exception ; * config
1 parent af61482 commit 629da78

File tree

11 files changed

+50
-17
lines changed

11 files changed

+50
-17
lines changed

src/GraphQl/Serializer/Exception/ValidationExceptionNormalizer.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313

1414
namespace ApiPlatform\GraphQl\Serializer\Exception;
1515

16+
use ApiPlatform\Metadata\Exception\RuntimeException;
17+
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface;
1618
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
1719
use GraphQL\Error\Error;
1820
use GraphQL\Error\FormattedError;
19-
use Symfony\Component\Form\Exception\RuntimeException;
2021
use Symfony\Component\HttpFoundation\Response;
2122
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2223

@@ -38,7 +39,7 @@ public function __construct(private readonly array $exceptionToStatus = [])
3839
public function normalize(mixed $object, ?string $format = null, array $context = []): array
3940
{
4041
$validationException = $object->getPrevious();
41-
if (!$validationException instanceof ConstraintViolationListAwareExceptionInterface) {
42+
if (!($validationException instanceof ConstraintViolationListAwareExceptionInterface || $validationException instanceof LegacyConstraintViolationListAwareExceptionInterface)) {
4243
throw new RuntimeException(sprintf('Object is not a "%s".', ConstraintViolationListAwareExceptionInterface::class));
4344
}
4445

src/JsonApi/Serializer/ErrorNormalizer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
use ApiPlatform\Problem\Serializer\ErrorNormalizerTrait;
1717
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
18-
use ApiPlatform\State\ApiResource\Error;
19-
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
18+
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface;
19+
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
2020
use Symfony\Component\ErrorHandler\Exception\FlattenException;
2121
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2222
use Symfony\Component\Serializer\Serializer;
@@ -48,7 +48,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
4848
{
4949
// TODO: in api platform 4 this will be the default, note that JSON:API is close to Problem so we should use the same normalizer
5050
if ($context['rfc_7807_compliant_errors'] ?? false) {
51-
if ($object instanceof ConstraintViolationListAwareExceptionInterface) {
51+
if ($object instanceof LegacyConstraintViolationListAwareExceptionInterface || $object instanceof ConstraintViolationListAwareExceptionInterface) {
5252
// TODO: return ['errors' => $this->constraintViolationListNormalizer(...)]
5353
return $this->constraintViolationListNormalizer->normalize($object->getConstraintViolationList(), $format, $context);
5454
}

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,7 @@ private function getFormats(array $configFormats): array
816816
private function registerValidatorConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
817817
{
818818
if (interface_exists(ValidatorInterface::class)) {
819+
$container->setParameter('api_platform.validator.legacy_validation_exception', $config['validator']['legacy_validation_exception'] ?? true);
819820
$loader->load('metadata/validator.xml');
820821
$loader->load('validator/validator.xml');
821822

src/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use ApiPlatform\Metadata\Put;
2323
use ApiPlatform\ParameterValidator\Exception\ValidationExceptionInterface;
2424
use ApiPlatform\Symfony\Controller\MainController;
25+
use ApiPlatform\Symfony\Validator\Exception\ValidationException as LegacyValidationException;
26+
use ApiPlatform\Validator\Exception\ValidationException;
2527
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
2628
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
2729
use Doctrine\ORM\EntityManagerInterface;
@@ -94,6 +96,7 @@ public function getConfigTreeBuilder(): TreeBuilder
9496
->children()
9597
->variableNode('serialize_payload_fields')->defaultValue([])->info('Set to null to serialize all payload fields when a validation error is thrown, or set the fields you want to include explicitly.')->end()
9698
->booleanNode('query_parameter_validation')->defaultValue(true)->end()
99+
->booleanNode('legacy_validation_exception')->defaultValue(true)->info('Uses the legacy "%s" instead of "%s".', LegacyValidationException::class, ValidationException::class)->end()
97100
->end()
98101
->end()
99102
->arrayNode('eager_loading')

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<service id="api_platform.validator" class="ApiPlatform\Symfony\Validator\Validator">
99
<argument type="service" id="validator" />
1010
<argument type="tagged_locator" tag="api_platform.validation_groups_generator" />
11+
<argument>%api_platform.validator.legacy_validation_exception%</argument>
1112
</service>
1213
<service id="ApiPlatform\Validator\ValidatorInterface" alias="api_platform.validator" />
1314

src/Symfony/EventListener/ErrorListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
2727
use ApiPlatform\State\ApiResource\Error;
2828
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
29-
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
29+
use ApiPlatform\State\Util\RequestAttributesExtractor;
30+
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface;
3031
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
3132
use Negotiation\Negotiator;
3233
use Psr\Log\LoggerInterface;
@@ -192,7 +193,7 @@ private function getStatusCode(?HttpOperation $apiOperation, Request $request, ?
192193
return 400;
193194
}
194195

195-
if ($exception instanceof ConstraintViolationListAwareExceptionInterface) {
196+
if ($exception instanceof ConstraintViolationListAwareExceptionInterface || $exception instanceof LegacyConstraintViolationListAwareExceptionInterface) {
196197
return 422;
197198
}
198199

src/Symfony/Validator/Exception/ConstraintViolationListAwareExceptionInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
/**
2020
* An exception which has a constraint violation list.
21+
*
22+
* @deprecated use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface
2123
*/
2224
interface ConstraintViolationListAwareExceptionInterface extends ExceptionInterface
2325
{

src/Symfony/Validator/Exception/ValidationException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use ApiPlatform\Metadata\ErrorResource;
1919
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
2020
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
21-
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface as ApiPlatformConstraintViolationListAwareExceptionInterface;
21+
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
2222
use ApiPlatform\Validator\Exception\ValidationException as BaseValidationException;
2323
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
2424
use Symfony\Component\WebLink\Link;
@@ -71,6 +71,6 @@
7171
],
7272
graphQlOperations: []
7373
)]
74-
final class ValidationException extends BaseValidationException implements ConstraintViolationListAwareExceptionInterface, ApiPlatformConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface, HttpExceptionInterface, SymfonyHttpExceptionInterface
74+
final class ValidationException extends BaseValidationException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface, HttpExceptionInterface, SymfonyHttpExceptionInterface
7575
{
7676
}

src/Symfony/Validator/Validator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
namespace ApiPlatform\Symfony\Validator;
1515

16-
use ApiPlatform\Symfony\Validator\Exception\ValidationException;
16+
use ApiPlatform\Symfony\Validator\Exception\ValidationException as LegacyValidationException;
17+
use ApiPlatform\Validator\Exception\ValidationException;
1718
use ApiPlatform\Validator\ValidatorInterface;
1819
use Psr\Container\ContainerInterface;
1920
use Symfony\Component\Validator\Constraints\GroupSequence;
@@ -28,7 +29,7 @@
2829
*/
2930
class Validator implements ValidatorInterface
3031
{
31-
public function __construct(private readonly SymfonyValidatorInterface $validator, private readonly ?ContainerInterface $container = null)
32+
public function __construct(private readonly SymfonyValidatorInterface $validator, private readonly ?ContainerInterface $container = null, private readonly ?bool $legacyValidationException = true)
3233
{
3334
}
3435

@@ -57,6 +58,9 @@ public function validate(object $data, array $context = []): void
5758

5859
$violations = $this->validator->validate($data, null, $validationGroups);
5960
if (0 !== \count($violations)) {
61+
if (true === $this->legacyValidationException) {
62+
throw new LegacyValidationException($violations);
63+
}
6064
throw new ValidationException($violations);
6165
}
6266
}

tests/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
109109
'validator' => [
110110
'serialize_payload_fields' => [],
111111
'query_parameter_validation' => true,
112+
'legacy_validation_exception' => true,
112113
],
113114
'name_converter' => null,
114115
'enable_swagger' => true,

0 commit comments

Comments
 (0)