• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 9219481874

24 May 2024 06:10AM CUT coverage: 57.41% (+0.02%) from 57.392%
9219481874

push

github

web-flow
feat(openapi): allow optional request body content (#6374)

14 of 14 new or added lines in 1 file covered. (100.0%)

58 existing lines in 14 files now uncovered.

10026 of 17464 relevant lines covered (57.41%)

48.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Symfony/Validator/EventListener/ValidationExceptionListener.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Symfony\Validator\EventListener;
15

16
use ApiPlatform\Exception\FilterValidationException;
17
use ApiPlatform\Symfony\EventListener\ExceptionListener;
18
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as SymfonyConstraintViolationListAwareExceptionInterface;
19
use ApiPlatform\Util\ErrorFormatGuesser;
20
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
21
use ApiPlatform\Validator\Exception\ValidationException;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
24
use Symfony\Component\Serializer\SerializerInterface;
25

26
/**
27
 * Handles validation errors.
28
 * TODO: remove this class.
29
 *
30
 * @deprecated
31
 *
32
 * @author Kévin Dunglas <dunglas@gmail.com>
33
 */
34
final class ValidationExceptionListener
35
{
36
    public function __construct(private readonly SerializerInterface $serializer, private readonly array $errorFormats, private readonly array $exceptionToStatus = [], private readonly ?ExceptionListener $exceptionListener = null)
37
    {
38
    }
62✔
39

40
    /**
41
     * Returns a list of violations normalized in the Hydra format.
42
     */
43
    public function onKernelException(ExceptionEvent $event): void
44
    {
45
        // API Platform 3.2 handles every exception through the exception listener so we just skip this one
46
        if ($this->exceptionListener) {
22✔
UNCOV
47
            return;
2✔
48
        }
49

50
        trigger_deprecation('api-platform', '3.2', sprintf('The class "%s" is deprecated and will be removed in 4.x.', __CLASS__));
20✔
51

52
        $exception = $event->getThrowable();
20✔
53
        $hasConstraintViolationList = ($exception instanceof ConstraintViolationListAwareExceptionInterface || $exception instanceof SymfonyConstraintViolationListAwareExceptionInterface);
20✔
54
        if (!$hasConstraintViolationList && !$exception instanceof FilterValidationException) {
20✔
55
            return;
4✔
56
        }
57

58
        $exceptionClass = $exception::class;
16✔
59
        $statusCode = Response::HTTP_UNPROCESSABLE_ENTITY;
16✔
60

61
        foreach ($this->exceptionToStatus as $class => $status) {
16✔
62
            if (is_a($exceptionClass, $class, true)) {
4✔
63
                $statusCode = $status;
4✔
64

65
                break;
4✔
66
            }
67
        }
68

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

71
        $context = [];
16✔
72
        if ($exception instanceof ValidationException && ($errorTitle = $exception->getErrorTitle())) {
16✔
73
            $context['title'] = $errorTitle;
4✔
74
        }
75

76
        $event->setResponse(new Response(
16✔
77
            $this->serializer->serialize($hasConstraintViolationList ? $exception->getConstraintViolationList() : $exception, $format['key'], $context),
16✔
78
            $statusCode,
16✔
79
            [
16✔
80
                'Content-Type' => sprintf('%s; charset=utf-8', $format['value'][0]),
16✔
81
                'X-Content-Type-Options' => 'nosniff',
16✔
82
                'X-Frame-Options' => 'deny',
16✔
83
            ]
16✔
84
        ));
16✔
85
    }
86
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc