• 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

10.0
/src/GraphQl/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\GraphQl\Serializer;
15

16
use ApiPlatform\Api\IdentifiersExtractorInterface;
17
use ApiPlatform\Api\IriConverterInterface;
18
use ApiPlatform\Metadata\Util\ClassInfoTrait;
19
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
20
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
21
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
23
use Symfony\Component\Serializer\Serializer;
24

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

33
    public const FORMAT = 'graphql';
34
    public const ITEM_RESOURCE_CLASS_KEY = '#itemResourceClass';
35
    public const ITEM_IDENTIFIERS_KEY = '#itemIdentifiers';
36

37
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter, private readonly IdentifiersExtractorInterface $identifiersExtractor)
38
    {
39
    }
54✔
40

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

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

58
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
16✔
59
    }
60

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

75
        return $this->decorated instanceof BaseCacheableSupportsMethodInterface && $this->decorated->hasCacheableSupportsMethod();
×
76
    }
77

78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @throws UnexpectedValueException
82
     */
83
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
84
    {
85
        if (isset($context['api_resource'])) {
×
86
            $originalResource = $context['api_resource'];
×
87
            unset($context['api_resource']);
×
88
        }
89

90
        $data = $this->decorated->normalize($object, $format, $context);
×
91
        if (!\is_array($data)) {
×
92
            throw new UnexpectedValueException('Expected data to be an array.');
×
93
        }
94

95
        if (!isset($originalResource)) {
×
96
            return $data;
×
97
        }
98

99
        if (isset($data['id'])) {
×
100
            $data['_id'] = $data['id'];
×
101
            $data['id'] = $this->iriConverter->getIriFromResource($originalResource);
×
102
        }
103

104
        if (!($context['no_resolver_data'] ?? false)) {
×
105
            $data[self::ITEM_RESOURCE_CLASS_KEY] = $this->getObjectClass($originalResource);
×
106
            $data[self::ITEM_IDENTIFIERS_KEY] = $this->identifiersExtractor->getIdentifiersFromItem($originalResource);
×
107
        }
108

109
        return $data;
×
110
    }
111
}
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