• 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

66.67
/src/Problem/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\Problem\Serializer;
15

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

21
/**
22
 * Normalizes errors according to the API Problem spec (RFC 7807).
23
 *
24
 * @see https://tools.ietf.org/html/rfc7807
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 */
28
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
29
{
30
    use ErrorNormalizerTrait;
31
    public const FORMAT = 'jsonproblem';
32
    public const TYPE = 'type';
33
    public const TITLE = 'title';
34
    private array $defaultContext = [
35
        self::TYPE => 'https://tools.ietf.org/html/rfc2616#section-10',
36
        self::TITLE => 'An error occurred',
37
    ];
38

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

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

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

59
        return $data;
18✔
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) {
30✔
73
            return [
2✔
74
                \Exception::class => true,
2✔
75
                FlattenException::class => true,
2✔
76
            ];
2✔
77
        }
78

79
        return [];
30✔
80
    }
81

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

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