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

api-platform / core / 5650127293

pending completion
5650127293

push

github

web-flow
fix: don't implement deprecated CacheableSupportsMethodInterface with Symfony 6.3+ (#5696)

* fix: don't implement deprecated CacheableSupportsMethodInterface

* fix: a check, and add tests

* fix ApiGatewayNormalizerTest

* more fixes

* fix more tests

* fix lowest

* only trigger the deprecation for Symfony 6.3

167 of 167 new or added lines in 23 files covered. (100.0%)

10865 of 18368 relevant lines covered (59.15%)

19.9 hits per line

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

68.0
/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 ApiPlatform\Serializer\CacheableSupportsMethodInterface;
18
use Symfony\Component\ErrorHandler\Exception\FlattenException;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\Serializer\Serializer;
21

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

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

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

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

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

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

60
        return $data;
16✔
61
    }
62

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

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

80
        return [];
44✔
81
    }
82

83
    public function hasCacheableSupportsMethod(): bool
84
    {
85
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
86
            trigger_deprecation(
×
87
                'api-platform/core',
×
88
                '3.1',
×
89
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
90
                __METHOD__
×
91
            );
×
92
        }
93

94
        return true;
×
95
    }
96
}
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