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

api-platform / core / 3713134090

pending completion
3713134090

Pull #5254

github

GitHub
Merge b2ec54b3c into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

197 of 197 new or added lines in 5 files covered. (100.0%)

10372 of 12438 relevant lines covered (83.39%)

11.97 hits per line

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

85.0
/src/GraphQl/Serializer/Exception/ValidationExceptionNormalizer.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\GraphQl\Serializer\Exception;
15

16
use ApiPlatform\Symfony\Validator\Exception\ValidationException;
17
use GraphQL\Error\Error;
18
use GraphQL\Error\FormattedError;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
use Symfony\Component\Validator\ConstraintViolation;
22

23
/**
24
 * Normalize validation exceptions.
25
 *
26
 * @author Mahmood Bazdar <mahmood@bazdar.me>
27
 * @author Alan Poulain <contact@alanpoulain.eu>
28
 */
29
final class ValidationExceptionNormalizer implements NormalizerInterface
30
{
31
    public function __construct(private readonly array $exceptionToStatus = [])
32
    {
33
    }
28✔
34

35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function normalize(mixed $object, string $format = null, array $context = []): array
39
    {
40
        /** @var ValidationException */
41
        $validationException = $object->getPrevious();
1✔
42
        $error = FormattedError::createFromException($object);
1✔
43
        $error['message'] = $validationException->getMessage();
1✔
44

45
        $exceptionClass = $validationException::class;
1✔
46
        $statusCode = Response::HTTP_UNPROCESSABLE_ENTITY;
1✔
47

48
        foreach ($this->exceptionToStatus as $class => $status) {
1✔
49
            if (is_a($exceptionClass, $class, true)) {
×
50
                $statusCode = $status;
×
51

52
                break;
×
53
            }
54
        }
55
        $error['extensions']['status'] = $statusCode;
1✔
56
        $error['extensions']['category'] = 'user';
1✔
57
        $error['extensions']['violations'] = [];
1✔
58

59
        /** @var ConstraintViolation $violation */
60
        foreach ($validationException->getConstraintViolationList() as $violation) {
1✔
61
            $error['extensions']['violations'][] = [
1✔
62
                'path' => $violation->getPropertyPath(),
1✔
63
                'message' => $violation->getMessage(),
1✔
64
            ];
1✔
65
        }
66

67
        return $error;
1✔
68
    }
69

70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
74
    {
75
        return $data instanceof Error && $data->getPrevious() instanceof ValidationException;
21✔
76
    }
77
}
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