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 @@ -16,6 +16,7 @@
use ApiPlatform\Exception\FilterValidationException;
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
use ApiPlatform\Util\ErrorFormatGuesser;
use ApiPlatform\Validator\Exception\ValidationException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\Serializer\SerializerInterface;
Expand Down Expand Up @@ -53,8 +54,13 @@ public function onKernelException(ExceptionEvent $event): void

$format = ErrorFormatGuesser::guessErrorFormat($event->getRequest(), $this->errorFormats);

$context = [];
if ($exception instanceof ValidationException && ($errorTitle = $exception->getErrorTitle())) {
$context['title'] = $errorTitle;
}

$event->setResponse(new Response(
$this->serializer->serialize($exception instanceof ConstraintViolationListAwareExceptionInterface ? $exception->getConstraintViolationList() : $exception, $format['key']),
$this->serializer->serialize($exception instanceof ConstraintViolationListAwareExceptionInterface ? $exception->getConstraintViolationList() : $exception, $format['key'], $context),
$statusCode,
[
'Content-Type' => sprintf('%s; charset=utf-8', $format['value'][0]),
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Validator/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
final class ValidationException extends BaseValidationException implements ConstraintViolationListAwareExceptionInterface, \Stringable
{
public function __construct(private readonly ConstraintViolationListInterface $constraintViolationList, string $message = '', int $code = 0, \Exception $previous = null)
public function __construct(private readonly ConstraintViolationListInterface $constraintViolationList, string $message = '', int $code = 0, \Throwable $previous = null, ?string $errorTitle = null)
{
parent::__construct($message ?: $this->__toString(), $code, $previous);
parent::__construct($message ?: $this->__toString(), $code, $previous, $errorTitle);
}

public function getConstraintViolationList(): ConstraintViolationListInterface
Expand Down
9 changes: 9 additions & 0 deletions src/Validator/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@
*/
class ValidationException extends RuntimeException
{
public function __construct(string $message = '', int $code = 0, \Throwable $previous = null, protected readonly ?string $errorTitle = null)
{
parent::__construct($message, $code, $previous);
}

public function getErrorTitle(): ?string
{
return $this->errorTitle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testValidationException(): void
$list = new ConstraintViolationList([]);

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

$listener = new ValidationExceptionListener($serializerProphecy->reveal(), ['hydra' => ['application/ld+json']]);
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, new ValidationException($list));
Expand Down Expand Up @@ -89,7 +89,7 @@ public function getConstraintViolationList(): ConstraintViolationListInterface
};

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize($constraintViolationList, 'hydra')->willReturn($serializedConstraintViolationList)->shouldBeCalledOnce();
$serializerProphecy->serialize($constraintViolationList, 'hydra', [])->willReturn($serializedConstraintViolationList)->shouldBeCalledOnce();

$exceptionEvent = new ExceptionEvent(
$this->prophesize(HttpKernelInterface::class)->reveal(),
Expand Down Expand Up @@ -120,7 +120,7 @@ public function testValidationFilterException(): void
$exception = new FilterValidationException([], 'my message');

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize($exception, 'hydra')->willReturn($exceptionJson)->shouldBeCalled();
$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(), \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, $exception);
Expand All @@ -134,4 +134,25 @@ public function testValidationFilterException(): void
$this->assertSame('nosniff', $response->headers->get('X-Content-Type-Options'));
$this->assertSame('deny', $response->headers->get('X-Frame-Options'));
}

public function testValidationExceptionWithHydraTitle(): void
{
$exceptionJson = '{"foo": "bar"}';
$list = new ConstraintViolationList([]);

$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize($list, 'hydra', ['title' => 'foo'])->willReturn($exceptionJson)->shouldBeCalled();

$listener = new ValidationExceptionListener($serializerProphecy->reveal(), ['hydra' => ['application/ld+json']]);
$event = new ExceptionEvent($this->prophesize(HttpKernelInterface::class)->reveal(), new Request(), \defined(HttpKernelInterface::class.'::MAIN_REQUEST') ? HttpKernelInterface::MAIN_REQUEST : HttpKernelInterface::MASTER_REQUEST, new ValidationException($list, errorTitle: 'foo'));
$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'));
}
}