• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 15108390568

19 May 2025 08:42AM UTC coverage: 26.351% (+17.9%) from 8.423%
15108390568

push

github

web-flow
refactor(symfony): remove obsolete option `validator.query-parameter-validation` (#7139)

4 of 4 new or added lines in 1 file covered. (100.0%)

116 existing lines in 12 files now uncovered.

13582 of 51542 relevant lines covered (26.35%)

72.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.81
/src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.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\Metadata\Resource\Factory;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\FilterInterface;
19
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
20
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
21
use ApiPlatform\Metadata\Operation;
22
use ApiPlatform\Metadata\Parameter;
23
use ApiPlatform\Metadata\ParameterProviderFilterInterface;
24
use ApiPlatform\Metadata\Parameters;
25
use ApiPlatform\Metadata\PropertiesAwareInterface;
26
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
27
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
28
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
29
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
30
use ApiPlatform\Serializer\Filter\FilterInterface as SerializerFilterInterface;
31
use ApiPlatform\State\Util\StateOptionsTrait;
32
use Psr\Container\ContainerInterface;
33
use Psr\Log\LoggerInterface;
34
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
35
use Symfony\Component\TypeInfo\Type;
36

37
/**
38
 * Prepares Parameters documentation by reading its filter details and declaring an OpenApi parameter.
39
 *
40
 * @experimental
41
 */
42
final class ParameterResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
43
{
44
    use StateOptionsTrait;
45

46
    private array $localPropertyCache;
47

48
    public function __construct(
49
        private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory,
50
        private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory,
51
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
52
        private readonly ?ContainerInterface $filterLocator = null,
53
        private readonly ?NameConverterInterface $nameConverter = null,
54
        private readonly ?LoggerInterface $logger = null,
55
    ) {
56
    }
2,084✔
57

58
    public function create(string $resourceClass): ResourceMetadataCollection
59
    {
60
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
143✔
61

62
        foreach ($resourceMetadataCollection as $i => $resource) {
143✔
63
            $operations = $resource->getOperations();
136✔
64

65
            $internalPriority = -1;
136✔
66
            foreach ($operations as $operationName => $operation) {
136✔
67
                $parameters = $this->getDefaultParameters($operation, $resourceClass, $internalPriority);
136✔
68
                if (\count($parameters) > 0) {
136✔
69
                    $operations->add($operationName, $operation->withParameters($parameters));
31✔
70
                }
71
            }
72

73
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
136✔
74

75
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
136✔
76
                continue;
60✔
77
            }
78

79
            $internalPriority = -1;
111✔
80
            foreach ($graphQlOperations as $operationName => $operation) {
111✔
81
                $parameters = $this->getDefaultParameters($operation, $resourceClass, $internalPriority);
111✔
82
                if (\count($parameters) > 0) {
111✔
83
                    $graphQlOperations[$operationName] = $operation->withParameters($parameters);
4✔
84
                }
85
            }
86

87
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
111✔
88
        }
89

90
        return $resourceMetadataCollection;
143✔
91
    }
92

93
    /**
94
     * @return array{propertyNames: string[], properties: array<string, ApiProperty>}
95
     */
96
    private function getProperties(string $resourceClass, ?Parameter $parameter = null): array
97
    {
98
        $k = $resourceClass.($parameter?->getProperties() ? ($parameter->getKey() ?? '') : '');
31✔
99
        if (isset($this->localPropertyCache[$k])) {
31✔
100
            return $this->localPropertyCache[$k];
27✔
101
        }
102

103
        $propertyNames = [];
31✔
104
        $properties = [];
31✔
105
        foreach ($parameter?->getProperties() ?? $this->propertyNameCollectionFactory->create($resourceClass) as $property) {
31✔
106
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
27✔
107
            if ($propertyMetadata->isReadable()) {
27✔
108
                $propertyNames[] = $property;
27✔
109
                $properties[$property] = $propertyMetadata;
27✔
110
            }
111
        }
112

113
        $this->localPropertyCache[$k] = ['propertyNames' => $propertyNames, 'properties' => $properties];
31✔
114

115
        return $this->localPropertyCache[$k];
31✔
116
    }
117

118
    private function getDefaultParameters(Operation $operation, string $resourceClass, int &$internalPriority): Parameters
119
    {
120
        $propertyNames = $properties = [];
136✔
121
        $parameters = $operation->getParameters() ?? new Parameters();
136✔
122
        foreach ($parameters as $key => $parameter) {
136✔
123
            ['propertyNames' => $propertyNames, 'properties' => $properties] = $this->getProperties($resourceClass, $parameter);
31✔
124
            if (null === $parameter->getProvider() && (($f = $parameter->getFilter()) && $f instanceof ParameterProviderFilterInterface)) {
31✔
UNCOV
125
                $parameters->add($key, $parameter->withProvider($f->getParameterProvider()));
×
126
            }
127

128
            if (':property' === $key) {
31✔
129
                foreach ($propertyNames as $property) {
4✔
130
                    $converted = $this->nameConverter?->denormalize($property) ?? $property;
4✔
131
                    $propertyParameter = $this->setDefaults($converted, $parameter, $resourceClass, $properties, $operation);
4✔
132
                    $priority = $propertyParameter->getPriority() ?? $internalPriority--;
4✔
133
                    $parameters->add($converted, $propertyParameter->withPriority($priority)->withKey($converted));
4✔
134
                }
135

136
                $parameters->remove($key, $parameter::class);
4✔
137
                continue;
4✔
138
            }
139

140
            $key = $parameter->getKey() ?? $key;
31✔
141

142
            if (str_contains($key, ':property') || ((($f = $parameter->getFilter()) && is_a($f, PropertiesAwareInterface::class, true)) || $parameter instanceof PropertiesAwareInterface)) {
31✔
143
                $p = [];
12✔
144
                foreach ($propertyNames as $prop) {
12✔
145
                    $p[$this->nameConverter?->denormalize($prop) ?? $prop] = $prop;
10✔
146
                }
147

148
                $parameter = $parameter->withExtraProperties($parameter->getExtraProperties() + ['_properties' => $p]);
12✔
149
            }
150

151
            $parameter = $this->setDefaults($key, $parameter, $resourceClass, $properties, $operation);
31✔
152
            // We don't do any type cast yet, a query parameter or an header is always a string or a list of strings
153
            if (null === $parameter->getNativeType()) {
31✔
154
                // this forces the type to be only a list
155
                if ('array' === ($parameter->getSchema()['type'] ?? null)) {
27✔
156
                    $parameter = $parameter->withNativeType(Type::list(Type::string()));
4✔
157
                } else {
158
                    $parameter = $parameter->withNativeType(Type::union(Type::string(), Type::list(Type::string())));
27✔
159
                }
160
            }
161

162
            $priority = $parameter->getPriority() ?? $internalPriority--;
31✔
163
            $parameters->add($key, $parameter->withPriority($priority));
31✔
164
        }
165

166
        return $parameters;
136✔
167
    }
168

169
    private function addFilterMetadata(Parameter $parameter): Parameter
170
    {
171
        if (!($filterId = $parameter->getFilter())) {
31✔
172
            return $parameter;
14✔
173
        }
174

175
        if (!\is_object($filterId) && !$this->filterLocator->has($filterId)) {
23✔
UNCOV
176
            return $parameter;
×
177
        }
178

179
        $filter = \is_object($filterId) ? $filterId : $this->filterLocator->get($filterId);
23✔
180

181
        if ($filter instanceof ParameterProviderFilterInterface) {
23✔
UNCOV
182
            $parameter = $parameter->withProvider($filter::getParameterProvider());
×
183
        }
184

185
        if (!$filter) {
23✔
UNCOV
186
            return $parameter;
×
187
        }
188

189
        if (null === $parameter->getSchema() && $filter instanceof JsonSchemaFilterInterface && $schema = $filter->getSchema($parameter)) {
23✔
190
            $parameter = $parameter->withSchema($schema);
21✔
191
        }
192

193
        if (null === $parameter->getOpenApi() && $filter instanceof OpenApiParameterFilterInterface && ($openApiParameter = $filter->getOpenApiParameters($parameter))) {
23✔
194
            $parameter = $parameter->withOpenApi($openApiParameter);
13✔
195
        }
196

197
        return $parameter;
23✔
198
    }
199

200
    /**
201
     * @param array<string, ApiProperty> $properties
202
     */
203
    private function setDefaults(string $key, Parameter $parameter, string $resourceClass, array $properties, Operation $operation): Parameter
204
    {
205
        if (null === $parameter->getKey()) {
31✔
206
            $parameter = $parameter->withKey($key);
31✔
207
        }
208

209
        $filter = $parameter->getFilter();
31✔
210
        if (\is_string($filter) && $this->filterLocator->has($filter)) {
31✔
211
            $filter = $this->filterLocator->get($filter);
9✔
212
        }
213

214
        if ($filter instanceof SerializerFilterInterface && null === $parameter->getProvider()) {
31✔
215
            $parameter = $parameter->withProvider('api_platform.serializer.filter_parameter_provider');
4✔
216
        }
217
        $currentKey = $key;
31✔
218
        if (null === $parameter->getProperty() && isset($properties[$key])) {
31✔
219
            $parameter = $parameter->withProperty($key);
19✔
220
        }
221

222
        if (null === $parameter->getProperty() && $this->nameConverter && ($nameConvertedKey = $this->nameConverter->normalize($key)) && isset($properties[$nameConvertedKey])) {
31✔
UNCOV
223
            $parameter = $parameter->withProperty($key)->withExtraProperties(['_query_property' => $nameConvertedKey] + $parameter->getExtraProperties());
×
UNCOV
224
            $currentKey = $nameConvertedKey;
×
225
        }
226

227
        if ($this->nameConverter && $property = $parameter->getProperty()) {
31✔
228
            $parameter = $parameter->withProperty($this->nameConverter->normalize($property));
23✔
229
        }
230

231
        if (isset($properties[$currentKey]) && ($eloquentRelation = ($properties[$currentKey]->getExtraProperties()['eloquent_relation'] ?? null)) && isset($eloquentRelation['foreign_key'])) {
31✔
UNCOV
232
            $parameter = $parameter->withExtraProperties(['_query_property' => $eloquentRelation['foreign_key']] + $parameter->getExtraProperties());
×
233
        }
234

235
        $parameter = $this->addFilterMetadata($parameter);
31✔
236

237
        if ($filter instanceof FilterInterface) {
31✔
238
            try {
239
                return $this->getLegacyFilterMetadata($parameter, $operation, $filter);
23✔
240
            } catch (RuntimeException $exception) {
14✔
241
                $this->logger?->alert($exception->getMessage(), ['exception' => $exception]);
14✔
242

243
                return $parameter;
14✔
244
            }
245
        }
246

247
        return $parameter;
14✔
248
    }
249

250
    private function getLegacyFilterMetadata(Parameter $parameter, Operation $operation, FilterInterface $filter): Parameter
251
    {
252
        $description = $filter->getDescription($this->getStateOptionsClass($operation, $operation->getClass()));
23✔
253
        $key = $parameter->getKey();
11✔
254
        if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
11✔
UNCOV
255
            $parameter = $parameter->withSchema($schema);
×
256
        }
257

258
        if (null === $parameter->getProperty() && ($property = $description[$key]['property'] ?? null)) {
11✔
UNCOV
259
            $parameter = $parameter->withProperty($property);
×
260
        }
261

262
        if (null === $parameter->getRequired() && ($required = $description[$key]['required'] ?? null)) {
11✔
UNCOV
263
            $parameter = $parameter->withRequired($required);
×
264
        }
265

266
        if (null === $parameter->getOpenApi() && ($openApi = $description[$key]['openapi'] ?? null) && $openApi instanceof OpenApiParameter) {
11✔
UNCOV
267
            $parameter = $parameter->withOpenApi($openApi);
×
268
        }
269

270
        return $parameter;
11✔
271
    }
272
}
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