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

api-platform / core / 5644825543

pending completion
5644825543

push

github

web-flow
feat(serializer): support for getSupportedTypes (symfony 6.3) (#5672)

109 of 109 new or added lines in 29 files covered. (100.0%)

10881 of 18245 relevant lines covered (59.64%)

20.04 hits per line

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

78.95
/src/JsonApi/Serializer/ErrorNormalizer.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\JsonApi\Serializer;
15

16
use ApiPlatform\Problem\Serializer\ErrorNormalizerTrait;
17
use Symfony\Component\ErrorHandler\Exception\FlattenException;
18
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20

21
/**
22
 * Converts {@see \Exception} or {@see FlattenException} or to a JSON API error representation.
23
 *
24
 * @author Héctor Hurtarte <hectorh30@gmail.com>
25
 */
26
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
27
{
28
    use ErrorNormalizerTrait;
29

30
    public const FORMAT = 'jsonapi';
31
    public const TITLE = 'title';
32
    private array $defaultContext = [
33
        self::TITLE => 'An error occurred',
34
    ];
35

36
    public function __construct(private readonly bool $debug = false, array $defaultContext = [])
37
    {
38
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
72✔
39
    }
40

41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function normalize(mixed $object, string $format = null, array $context = []): array
45
    {
46
        $data = [
16✔
47
            'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
16✔
48
            'description' => $this->getErrorMessage($object, $context, $this->debug),
16✔
49
        ];
16✔
50

51
        if (null !== $errorCode = $this->getErrorCode($object)) {
16✔
52
            $data['code'] = $errorCode;
4✔
53
        }
54

55
        if ($this->debug && null !== $trace = $object->getTrace()) {
16✔
56
            $data['trace'] = $trace;
6✔
57
        }
58

59
        return $data;
16✔
60
    }
61

62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
66
    {
67
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException);
2✔
68
    }
69

70
    public function getSupportedTypes($format): array
71
    {
72
        if (self::FORMAT === $format) {
42✔
73
            return [
×
74
                \Exception::class => true,
×
75
                FlattenException::class => true,
×
76
            ];
×
77
        }
78

79
        return [];
42✔
80
    }
81

82
    public function hasCacheableSupportsMethod(): bool
83
    {
84
        trigger_deprecation('api-platform/core', '3.1', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
2✔
85

86
        return true;
2✔
87
    }
88
}
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

© 2026 Coveralls, Inc