Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/2.6' into merge-26
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed May 17, 2021
2 parents de5988d + bb59e4c commit fe05990
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\Bridge\Symfony\Validator\EventListener;

use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
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 ConstraintViolationListAwareExceptionInterface) {
if (!$exception instanceof ConstraintViolationListAwareExceptionInterface && !$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 ConstraintViolationListAwareExceptionInterface ? $exception->getConstraintViolationList() : $exception, $format['key']),
$statusCode,
[
'Content-Type' => sprintf('%s; charset=utf-8', $format['value'][0]),
Expand Down
4 changes: 2 additions & 2 deletions src/Util/Inflector.php
Expand Up @@ -42,14 +42,14 @@ private static function getInstance(): InflectorObject
*/
public static function tableize(string $word): string
{
return class_exists(LegacyInflector::class) ? LegacyInflector::tableize($word) : self::getInstance()->tableize($word);
return class_exists(InflectorFactory::class) ? self::getInstance()->tableize($word) : LegacyInflector::tableize($word); // @phpstan-ignore-line
}

/**
* @see InflectorObject::pluralize()
*/
public static function pluralize(string $word): string
{
return class_exists(LegacyInflector::class) ? LegacyInflector::pluralize($word) : self::getInstance()->pluralize($word);
return class_exists(InflectorFactory::class) ? self::getInstance()->pluralize($word) : LegacyInflector::pluralize($word); // @phpstan-ignore-line
}
}
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Core\Bridge\Symfony\Validator\EventListener\ValidationExceptionListener;
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
use ApiPlatform\Core\Exception\FilterValidationException;
use ApiPlatform\Core\Tests\ProphecyTrait;
use ApiPlatform\Core\Validator\Exception\ValidationException as BaseValidationException;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -111,4 +112,25 @@ public function getConstraintViolationList(): ConstraintViolationListInterface
self::assertSame('nosniff', $response->headers->get('X-Content-Type-Options'));
self::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'));
}
}
3 changes: 2 additions & 1 deletion tests/Fixtures/app/AppKernel.php
Expand Up @@ -19,6 +19,7 @@
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
use Doctrine\Common\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle;
use Nelmio\ApiDocBundle\NelmioApiDocBundle;
use Symfony\Bridge\Doctrine\Types\UuidType;
Expand Down Expand Up @@ -57,7 +58,7 @@ public function __construct(string $environment, bool $debug)

// patch for old versions of Doctrine Inflector, to delete when we'll drop support for v1
// see https://github.com/doctrine/inflector/issues/147#issuecomment-628807276
if (class_exists(Inflector::class)) {
if (!class_exists(InflectorFactory::class)) { // @phpstan-ignore-next-line
Inflector::rules('plural', ['/taxon/i' => 'taxa']);
}
}
Expand Down

0 comments on commit fe05990

Please sign in to comment.