• 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

8.82
/src/JsonApi/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\JsonApi\Serializer;
15

16
use ApiPlatform\Api\IriConverterInterface;
17
use ApiPlatform\Api\ResourceClassResolverInterface;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\Metadata\Util\ClassInfoTrait;
20
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
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 JSON API metadata when appropriate, but otherwise
27
 * just passes through to the decorated normalizer.
28
 */
29
final class ObjectNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
30
{
31
    use ClassInfoTrait;
32

33
    public const FORMAT = 'jsonapi';
34

35
    public function __construct(private readonly NormalizerInterface $decorated, private readonly IriConverterInterface $iriConverter, private readonly ResourceClassResolverInterface $resourceClassResolver, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory)
36
    {
37
    }
54✔
38

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

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

56
        return self::FORMAT === $format ? $this->decorated->getSupportedTypes($format) : [];
16✔
57
    }
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) || isset($context['api_attribute'])) {
×
85
            return $data;
×
86
        }
87

88
        if (isset($originalResource)) {
×
89
            $resourceClass = $this->resourceClassResolver->getResourceClass($originalResource);
×
90
            $resourceData = [
×
91
                'id' => $this->iriConverter->getIriFromResource($originalResource),
×
92
                'type' => $this->resourceMetadataFactory->create($resourceClass)->getOperation()->getShortName(),
×
93
            ];
×
94
        } else {
95
            $resourceData = [
×
96
                'id' => $this->iriConverter->getIriFromResource($object),
×
97
                'type' => (new \ReflectionClass($this->getObjectClass($object)))->getShortName(),
×
98
            ];
×
99
        }
100

101
        if ($data) {
×
102
            $resourceData['attributes'] = $data;
×
103
        }
104

105
        return ['data' => $resourceData];
×
106
    }
107
}
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