• 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

18.18
/src/Hal/Serializer/ObjectNormalizer.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\Hal\Serializer;
15

16
use ApiPlatform\Api\IriConverterInterface;
17
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
18
use Symfony\Component\Serializer\Exception\LogicException;
19
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
20
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22
use Symfony\Component\Serializer\Serializer;
23

24
/**
25
 * Decorates the output with JSON HAL metadata when appropriate, but otherwise
26
 * just passes through to the decorated normalizer.
27
 */
28
final class ObjectNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
29
{
30
    public const FORMAT = 'jsonhal';
31

32
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter)
33
    {
34
    }
58✔
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
40
    {
41
        return self::FORMAT === $format && $this->decorated->supportsNormalization($data, $format, $context);
2✔
42
    }
43

44
    public function getSupportedTypes($format): array
45
    {
46
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
47
        if (!method_exists($this->decorated, 'getSupportedTypes')) {
16✔
48
            return [
×
49
                '*' => $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod(),
×
50
            ];
×
51
        }
52

53
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
16✔
54
    }
55

56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function hasCacheableSupportsMethod(): bool
60
    {
61
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
62
            trigger_deprecation(
×
63
                'api-platform/core',
×
64
                '3.1',
×
65
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
66
                __METHOD__
×
67
            );
×
68
        }
69

70
        return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
×
71
    }
72

73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
77
    {
78
        if (isset($context['api_resource'])) {
×
79
            $originalResource = $context['api_resource'];
×
80
            unset($context['api_resource']);
×
81
        }
82

83
        $data = $this->decorated->normalize($object, $format, $context);
×
84
        if (!\is_array($data)) {
×
85
            return $data;
×
86
        }
87

88
        if (!isset($originalResource)) {
×
89
            return $data;
×
90
        }
91

92
        $metadata = [
×
93
            '_links' => [
×
94
                'self' => [
×
95
                    'href' => $this->iriConverter->getIriFromResource($originalResource),
×
96
                ],
×
97
            ],
×
98
        ];
×
99

100
        return $metadata + $data;
×
101
    }
102

103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool
107
    {
108
        // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format
109
        return self::FORMAT === $format;
2✔
110
    }
111

112
    /**
113
     * {@inheritdoc}
114
     *
115
     * @throws LogicException
116
     */
117
    public function denormalize(mixed $data, string $type, string $format = null, array $context = []): mixed
118
    {
119
        throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT));
2✔
120
    }
121
}
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