Skip to content

Commit

Permalink
Merge 4681691 into b5fe96f
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed May 6, 2021
2 parents b5fe96f + 4681691 commit c58d17b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\Bridge\Symfony\Validator\EventListener;

use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
use ApiPlatform\Core\Exception\FilterValidationException;
use ApiPlatform\Core\Util\ErrorFormatGuesser;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
Expand Down Expand Up @@ -43,7 +44,7 @@ public function __construct(SerializerInterface $serializer, array $errorFormats
public function onKernelException(ExceptionEvent $event): void
{
$exception = method_exists($event, 'getThrowable') ? $event->getThrowable() : $event->getException(); // @phpstan-ignore-line
if (!$exception instanceof ValidationException) {
if (!$exception instanceof ValidationException && !$exception instanceof FilterValidationException) {
return;
}
$exceptionClass = \get_class($exception);
Expand All @@ -60,7 +61,7 @@ public function onKernelException(ExceptionEvent $event): void
$format = ErrorFormatGuesser::guessErrorFormat($event->getRequest(), $this->errorFormats);

$event->setResponse(new Response(
$this->serializer->serialize($exception->getConstraintViolationList(), $format['key']),
$this->serializer->serialize($exception instanceof ValidationException ? $exception->getConstraintViolationList() : $exception, $format['key']),
$statusCode,
[
'Content-Type' => sprintf('%s; charset=utf-8', $format['value'][0]),
Expand Down
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\Core\Bridge\Symfony\Validator\EventListener\ValidationExceptionListener;
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
use ApiPlatform\Core\Exception\FilterValidationException;
use ApiPlatform\Core\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -62,4 +63,25 @@ public function testValidationException()
$this->assertSame('nosniff', $response->headers->get('X-Content-Type-Options'));
$this->assertSame('deny', $response->headers->get('X-Frame-Options'));
}

public function testValidationFilterException()
{
$exceptionJson = '{"message": "my message"}';
$exception = new FilterValidationException([], 'my message');

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize($exception, 'hydra')->willReturn($exceptionJson)->shouldBeCalled();

$listener = new ValidationExceptionListener($serializerProphecy->reveal(), ['hydra' => ['application/ld+json']]);
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), HttpKernelInterface::MASTER_REQUEST, $exception);
$listener->onKernelException($event);

$response = $event->getResponse();
$this->assertInstanceOf(Response::class, $response);
$this->assertSame($exceptionJson, $response->getContent());
$this->assertSame(Response::HTTP_UNPROCESSABLE_ENTITY, $response->getStatusCode());
$this->assertSame('application/ld+json; charset=utf-8', $response->headers->get('Content-Type'));
$this->assertSame('nosniff', $response->headers->get('X-Content-Type-Options'));
$this->assertSame('deny', $response->headers->get('X-Frame-Options'));
}
}

0 comments on commit c58d17b

Please sign in to comment.