• 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

53.57
/src/JsonLd/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\JsonLd\Serializer;
15

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

24
/**
25
 * Decorates the output with JSON-LD metadata when appropriate, but otherwise just
26
 * passes through to the decorated normalizer.
27
 */
28
final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
29
{
30
    use JsonLdContextTrait;
31

32
    public const FORMAT = 'jsonld';
33

34
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter, private AnonymousContextBuilderInterface $anonymousContextBuilder)
35
    {
36
    }
62✔
37

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

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

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

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

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

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

82
        /*
83
         * Converts the normalized data array of a resource into an IRI, if the
84
         * normalized data array is empty.
85
         *
86
         * This is useful when traversing from a non-resource towards an attribute
87
         * which is a resource, as we do not have the benefit of {@see ApiProperty::isReadableLink}.
88
         *
89
         * It must not be propagated to resources, as {@see ApiProperty::isReadableLink}
90
         * should take effect.
91
         */
92
        $context['api_empty_resource_as_iri'] = true;
8✔
93

94
        $data = $this->decorated->normalize($object, $format, $context);
8✔
95
        if (!\is_array($data) || !$data) {
8✔
96
            return $data;
2✔
97
        }
98

99
        if (isset($originalResource)) {
6✔
100
            try {
101
                $context['output']['iri'] = $this->iriConverter->getIriFromResource($originalResource);
4✔
102
            } catch (InvalidArgumentException) {
×
103
                // The original resource has no identifiers
104
            }
105
            $context['api_resource'] = $originalResource;
4✔
106
        }
107

108
        $metadata = $this->createJsonLdContext($this->anonymousContextBuilder, $object, $context);
6✔
109

110
        return $metadata + $data;
6✔
111
    }
112
}
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