• 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

71.74
/src/Hydra/Serializer/CollectionFiltersNormalizer.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\Hydra\Serializer;
15

16
use ApiPlatform\Api\FilterInterface;
17
use ApiPlatform\Api\FilterLocatorTrait;
18
use ApiPlatform\Api\ResourceClassResolverInterface;
19
use ApiPlatform\Doctrine\Orm\State\Options;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
22
use Psr\Container\ContainerInterface;
23
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
24
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
25
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface as BaseCacheableSupportsMethodInterface;
26
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
27
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
28
use Symfony\Component\Serializer\Serializer;
29

30
/**
31
 * Enhances the result of collection by adding the filters applied on collection.
32
 *
33
 * @author Samuel ROZE <samuel.roze@gmail.com>
34
 */
35
final class CollectionFiltersNormalizer implements NormalizerInterface, NormalizerAwareInterface, CacheableSupportsMethodInterface
36
{
37
    use FilterLocatorTrait;
38

39
    /**
40
     * @param ContainerInterface $filterLocator The new filter locator or the deprecated filter collection
41
     */
42
    public function __construct(private readonly NormalizerInterface $collectionNormalizer, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory, private readonly ResourceClassResolverInterface $resourceClassResolver, ContainerInterface $filterLocator)
43
    {
44
        $this->setFilterLocator($filterLocator);
68✔
45
    }
46

47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
51
    {
52
        return $this->collectionNormalizer->supportsNormalization($data, $format, $context);
22✔
53
    }
54

55
    public function getSupportedTypes($format): array
56
    {
57
        // @deprecated remove condition when support for symfony versions under 6.3 is dropped
58
        if (!method_exists($this->collectionNormalizer, 'getSupportedTypes')) {
30✔
59
            return ['*' => $this->collectionNormalizer instanceof BaseCacheableSupportsMethodInterface && $this->collectionNormalizer->hasCacheableSupportsMethod()];
×
60
        }
61

62
        return $this->collectionNormalizer->getSupportedTypes($format);
30✔
63
    }
64

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

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

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
83
    {
84
        if (($context[AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS] ?? false) && $object instanceof \ArrayObject && !\count($object)) {
32✔
85
            return $object;
×
86
        }
87

88
        $data = $this->collectionNormalizer->normalize($object, $format, $context);
32✔
89
        if (!isset($context['resource_class']) || isset($context['api_sub_level'])) {
32✔
90
            return $data;
10✔
91
        }
92

93
        if (!\is_array($data)) {
22✔
94
            throw new UnexpectedValueException('Expected data to be an array');
×
95
        }
96
        $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class']);
22✔
97
        $operation = $context['operation'] ?? $this->resourceMetadataCollectionFactory->create($resourceClass)->getOperation($context['operation_name'] ?? null);
22✔
98
        $resourceFilters = $operation->getFilters();
22✔
99
        if (!$resourceFilters) {
22✔
100
            return $data;
14✔
101
        }
102
        $requestParts = parse_url($context['request_uri'] ?? '');
8✔
103
        if (!\is_array($requestParts)) {
8✔
104
            return $data;
×
105
        }
106
        $currentFilters = [];
8✔
107
        foreach ($resourceFilters as $filterId) {
8✔
108
            if ($filter = $this->getFilter($filterId)) {
8✔
109
                $currentFilters[] = $filter;
6✔
110
            }
111
        }
112

113
        if (($options = $operation?->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
8✔
114
            $resourceClass = $options->getEntityClass();
×
115
        }
116

117
        if ($currentFilters) {
8✔
118
            $data['hydra:search'] = $this->getSearch($resourceClass, $requestParts, $currentFilters);
6✔
119
        }
120

121
        return $data;
8✔
122
    }
123

124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function setNormalizer(NormalizerInterface $normalizer): void
128
    {
129
        if ($this->collectionNormalizer instanceof NormalizerAwareInterface) {
54✔
130
            $this->collectionNormalizer->setNormalizer($normalizer);
54✔
131
        }
132
    }
133

134
    /**
135
     * Returns the content of the Hydra search property.
136
     *
137
     * @param FilterInterface[] $filters
138
     */
139
    private function getSearch(string $resourceClass, array $parts, array $filters): array
140
    {
141
        $variables = [];
6✔
142
        $mapping = [];
6✔
143
        foreach ($filters as $filter) {
6✔
144
            foreach ($filter->getDescription($resourceClass) as $variable => $data) {
6✔
145
                $variables[] = $variable;
6✔
146
                $mapping[] = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $data['property'], 'required' => $data['required']];
6✔
147
            }
148
        }
149

150
        return ['@type' => 'hydra:IriTemplate', 'hydra:template' => sprintf('%s{?%s}', $parts['path'], implode(',', $variables)), 'hydra:variableRepresentation' => 'BasicRepresentation', 'hydra:mapping' => $mapping];
6✔
151
    }
152
}
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