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

api-platform / core / 13925245599

18 Mar 2025 02:06PM UTC coverage: 61.074% (-0.9%) from 61.973%
13925245599

Pull #7031

github

web-flow
Merge 8990d8b26 into 7cb5a6db8
Pull Request #7031: fix: header parameter should be case insensitive

3 of 4 new or added lines in 1 file covered. (75.0%)

167 existing lines in 27 files now uncovered.

11314 of 18525 relevant lines covered (61.07%)

51.02 hits per line

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

96.3
/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\ParameterValidator\Exception\ValidationException as ParameterValidationException;
18
use ApiPlatform\Symfony\EventListener\ExceptionListener;
19
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as SymfonyConstraintViolationListAwareExceptionInterface;
20
use ApiPlatform\Util\ErrorFormatGuesser;
21
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
22
use ApiPlatform\Validator\Exception\ValidationException;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
25
use Symfony\Component\Serializer\SerializerInterface;
26

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

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

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

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

59
        $exceptionClass = $exception::class;
12✔
60
        $statusCode = Response::HTTP_UNPROCESSABLE_ENTITY;
12✔
61

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

66
                break;
3✔
67
            }
68
        }
69

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

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

77
        $event->setResponse(new Response(
12✔
78
            $this->serializer->serialize($hasConstraintViolationList ? $exception->getConstraintViolationList() : $exception, $format['key'], $context),
12✔
79
            $statusCode,
12✔
80
            [
12✔
81
                'Content-Type' => \sprintf('%s; charset=utf-8', $format['value'][0]),
12✔
82
                'X-Content-Type-Options' => 'nosniff',
12✔
83
                'X-Frame-Options' => 'deny',
12✔
84
            ]
12✔
85
        ));
12✔
86
    }
87
}
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