From 3df6b4bce723ff03c2a381e4e29ee1e71b5788fa Mon Sep 17 00:00:00 2001 From: soyuka Date: Mon, 18 Sep 2023 11:35:03 +0200 Subject: [PATCH] fix: profiler can not serialize closure --- .../Bundle/Resources/config/legacy/events.xml | 2 +- src/Symfony/EventListener/ErrorListener.php | 19 ++++++++++++++++++- .../ValidationExceptionListener.php | 7 ++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bundle/Resources/config/legacy/events.xml b/src/Symfony/Bundle/Resources/config/legacy/events.xml index a8d7ac3ecc1..ef204daf222 100644 --- a/src/Symfony/Bundle/Resources/config/legacy/events.xml +++ b/src/Symfony/Bundle/Resources/config/legacy/events.xml @@ -61,7 +61,7 @@ %api_platform.error_formats% %api_platform.exception_to_status% - + diff --git a/src/Symfony/EventListener/ErrorListener.php b/src/Symfony/EventListener/ErrorListener.php index 9b154cdbf97..b7f94dc1d2a 100644 --- a/src/Symfony/EventListener/ErrorListener.php +++ b/src/Symfony/EventListener/ErrorListener.php @@ -18,6 +18,7 @@ use ApiPlatform\Metadata\Error as ErrorOperation; use ApiPlatform\Metadata\Exception\ProblemExceptionInterface; use ApiPlatform\Metadata\HttpOperation; +use ApiPlatform\Metadata\Operation; use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; use ApiPlatform\Metadata\ResourceClassResolverInterface; use ApiPlatform\Metadata\Util\ContentNegotiationTrait; @@ -111,7 +112,10 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re } if (!$operation->getProvider()) { - $operation = $operation->withProvider(provider: fn () => 'jsonapi' === $format && $errorResource instanceof ConstraintViolationListAwareExceptionInterface ? $errorResource->getConstraintViolationList() : $errorResource); + $data = 'jsonapi' === $format && $errorResource instanceof ConstraintViolationListAwareExceptionInterface ? $errorResource->getConstraintViolationList() : $errorResource; + $dup->attributes->set('_api_error_resource', $data); + $operation = $operation->withExtraProperties(['_api_error_resource' => $data]) + ->withProvider([self::class, 'provide']); } // For our swagger Ui errors @@ -216,4 +220,17 @@ private function getFormatOperation(?string $format): string default => '_api_errors_problem' }; } + + public static function provide(Operation $operation, array $uriVariables = [], array $context = []) + { + if ($data = ($context['request'] ?? null)?->attributes->get('_api_error_resource')) { + return $data; + } + + if ($data = $operation->getExtraProperties()['_api_error_resource'] ?? null) { + return $data; + } + + throw new \LogicException(sprintf('We could not find the thrown exception in the %s.', self::class)); + } } diff --git a/src/Symfony/Validator/EventListener/ValidationExceptionListener.php b/src/Symfony/Validator/EventListener/ValidationExceptionListener.php index 10249427edd..36b370e5ab3 100644 --- a/src/Symfony/Validator/EventListener/ValidationExceptionListener.php +++ b/src/Symfony/Validator/EventListener/ValidationExceptionListener.php @@ -24,7 +24,9 @@ /** * Handles validation errors. - * todo remove this class. + * TODO: remove this class. + * + * @deprecated * * @author Kévin Dunglas */ @@ -39,9 +41,8 @@ public function __construct(private readonly SerializerInterface $serializer, pr */ public function onKernelException(ExceptionEvent $event): void { + // API Platform 3.2 handles every exception through the exception listener so we just skip this one if ($this->exceptionListener) { - $this->exceptionListener->onKernelException($event); - return; }