• 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

83.02
/src/Serializer/AbstractCollectionNormalizer.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\Serializer;
15

16
use ApiPlatform\Api\ResourceClassResolverInterface;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
19
use ApiPlatform\State\Pagination\PaginatorInterface;
20
use ApiPlatform\State\Pagination\PartialPaginatorInterface;
21
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
22
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
23
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
24
use Symfony\Component\Serializer\Serializer;
25

26
/**
27
 * Base collection normalizer.
28
 *
29
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
30
 */
31
abstract class AbstractCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
32
{
33
    use ContextTrait {
34
        initContext as protected;
35
    }
36
    use NormalizerAwareTrait;
37

38
    /**
39
     * This constant must be overridden in the child class.
40
     */
41
    // @noRector \Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector
42
    public const FORMAT = 'to-override';
43

44
    public function __construct(protected ResourceClassResolverInterface $resourceClassResolver, protected string $pageParameterName, protected ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null)
45
    {
46
    }
84✔
47

48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
52
    {
53
        return static::FORMAT === $format && is_iterable($data);
26✔
54
    }
55

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

67
        return true;
×
68
    }
69

70
    public function getSupportedTypes(?string $format): array
71
    {
72
        /*
73
         * At this point, support anything that is_iterable(), i.e. array|Traversable
74
         * for non-objects, symfony uses 'native-'.\gettype($data) :
75
         * https://github.com/tucksaun/symfony/blob/400685a68b00b0932f8ef41096578872b643099c/src/Symfony/Component/Serializer/Serializer.php#L254
76
         */
77
        if (static::FORMAT === $format) {
34✔
78
            return [
28✔
79
                'native-array' => true,
28✔
80
                '\Traversable' => true,
28✔
81
            ];
28✔
82
        }
83

84
        return [];
34✔
85
    }
86

87
    /**
88
     * {@inheritdoc}
89
     *
90
     * @param iterable $object
91
     */
92
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
93
    {
94
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
44✔
95
            return $this->normalizeRawCollection($object, $format, $context);
4✔
96
        }
97

98
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
40✔
99
        $context = $this->initContext($resourceClass, $context);
40✔
100
        $data = [];
40✔
101
        $paginationData = $this->getPaginationData($object, $context);
40✔
102

103
        if (($operation = $context['operation'] ?? null) && method_exists($operation, 'getItemUriTemplate')) {
40✔
104
            $context['item_uri_template'] = $operation->getItemUriTemplate();
16✔
105
        }
106

107
        // We need to keep this operation for serialization groups for later
108
        if (isset($context['operation'])) {
40✔
109
            $context['root_operation'] = $context['operation'];
16✔
110
        }
111

112
        if (isset($context['operation_name'])) {
40✔
113
            $context['root_operation_name'] = $context['operation_name'];
36✔
114
        }
115

116
        unset($context['operation']);
40✔
117
        unset($context['operation_type'], $context['operation_name']);
40✔
118

119
        $itemsData = $this->getItemsData($object, $format, $context);
40✔
120

121
        return array_merge_recursive($data, $paginationData, $itemsData);
38✔
122
    }
123

124
    /**
125
     * Normalizes a raw collection (not API resources).
126
     */
127
    protected function normalizeRawCollection(iterable $object, string $format = null, array $context = []): array|\ArrayObject
128
    {
129
        if (!$object && ($context[Serializer::EMPTY_ARRAY_AS_OBJECT] ?? false) && \is_array($object)) {
4✔
130
            return new \ArrayObject();
×
131
        }
132

133
        $data = [];
4✔
134
        foreach ($object as $index => $obj) {
4✔
135
            $data[$index] = $this->normalizer->normalize($obj, $format, $context);
2✔
136
        }
137

138
        return $data;
4✔
139
    }
140

141
    /**
142
     * Gets the pagination configuration.
143
     */
144
    protected function getPaginationConfig(iterable $object, array $context = []): array
145
    {
146
        $currentPage = $lastPage = $itemsPerPage = $pageTotalItems = $totalItems = null;
14✔
147
        $paginated = $paginator = false;
14✔
148

149
        if ($object instanceof PartialPaginatorInterface) {
14✔
150
            $paginated = $paginator = true;
8✔
151
            if ($object instanceof PaginatorInterface) {
8✔
152
                $paginated = 1. !== $lastPage = $object->getLastPage();
4✔
153
                $totalItems = $object->getTotalItems();
4✔
154
            } else {
155
                $pageTotalItems = (float) \count($object);
4✔
156
            }
157

158
            $currentPage = $object->getCurrentPage();
8✔
159
            $itemsPerPage = $object->getItemsPerPage();
8✔
160
        } elseif (is_countable($object)) {
6✔
161
            $totalItems = \count($object);
6✔
162
        }
163

164
        return [$paginator, $paginated, $currentPage, $itemsPerPage, $lastPage, $pageTotalItems, $totalItems];
14✔
165
    }
166

167
    protected function getOperation(array $context = []): Operation
168
    {
169
        $metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? '');
14✔
170

171
        return $metadata->getOperation($context['operation_name'] ?? null);
14✔
172
    }
173

174
    /**
175
     * Gets the pagination data.
176
     */
177
    abstract protected function getPaginationData(iterable $object, array $context = []): array;
178

179
    /**
180
     * Gets items data.
181
     */
182
    abstract protected function getItemsData(iterable $object, string $format = null, array $context = []): array;
183
}
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