• 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/Hydra/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\Hydra\Serializer;
15

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

23
/**
24
 * Converts {@see \Exception} or {@see FlattenException} to a Hydra error representation.
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 * @author Samuel ROZE <samuel.roze@gmail.com>
28
 */
29
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
30
{
31
    use ErrorNormalizerTrait;
32

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

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

42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
46
    {
47
        $data = [
14✔
48
            '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Error']),
14✔
49
            '@type' => 'hydra:Error',
14✔
50
            'hydra:title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
14✔
51
            'hydra:description' => $this->getErrorMessage($object, $context, $this->debug),
14✔
52
        ];
14✔
53

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

58
        return $data;
14✔
59
    }
60

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

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

78
        return [];
8✔
79
    }
80

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

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