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

api-platform / core / 17487610263

05 Sep 2025 08:12AM UTC coverage: 22.608% (+0.3%) from 22.319%
17487610263

push

github

web-flow
chore: remove @experimental flag from parameters (#7357)

12049 of 53296 relevant lines covered (22.61%)

26.21 hits per line

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

92.68
/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\Parameter\ValueCaster;
32
use ApiPlatform\State\Util\StateOptionsTrait;
33
use Psr\Container\ContainerInterface;
34
use Psr\Log\LoggerInterface;
35
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
36
use Symfony\Component\TypeInfo\Type;
37
use Symfony\Component\TypeInfo\TypeIdentifier;
38

39
/**
40
 * Prepares Parameters documentation by reading its filter details and declaring an OpenApi parameter.
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
    }
722✔
57

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

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

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

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

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

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

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

90
        return $resourceMetadataCollection;
112✔
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() ?? '') : '');
30✔
99
        if (isset($this->localPropertyCache[$k])) {
30✔
100
            return $this->localPropertyCache[$k];
26✔
101
        }
102

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

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

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

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

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

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

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

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

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

151
            $parameter = $this->setDefaults($key, $parameter, $resourceClass, $properties, $operation);
30✔
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()) {
30✔
154
                // this forces the type to be only a list
155
                if ('array' === ($parameter->getSchema()['type'] ?? null)) {
26✔
156
                    $parameter = $parameter->withNativeType(Type::list(Type::string()));
2✔
157
                } elseif ('string' === ($parameter->getSchema()['type'] ?? null)) {
26✔
158
                    $parameter = $parameter->withNativeType(Type::string());
12✔
159
                } elseif ('boolean' === ($parameter->getSchema()['type'] ?? null)) {
18✔
160
                    $parameter = $parameter->withNativeType(Type::bool());
4✔
161
                } elseif ('integer' === ($parameter->getSchema()['type'] ?? null)) {
16✔
162
                    $parameter = $parameter->withNativeType(Type::int());
2✔
163
                } elseif ('number' === ($parameter->getSchema()['type'] ?? null)) {
16✔
164
                    $parameter = $parameter->withNativeType(Type::float());
4✔
165
                } else {
166
                    $parameter = $parameter->withNativeType(Type::union(Type::string(), Type::list(Type::string())));
14✔
167
                }
168
            }
169

170
            if ($parameter->getCastToNativeType() && null === $parameter->getCastFn() && ($nativeType = $parameter->getNativeType())) {
30✔
171
                if ($nativeType->isIdentifiedBy(TypeIdentifier::BOOL)) {
2✔
172
                    $parameter = $parameter->withCastFn([ValueCaster::class, 'toBool']);
2✔
173
                }
174
                if ($nativeType->isIdentifiedBy(TypeIdentifier::INT)) {
2✔
175
                    $parameter = $parameter->withCastFn([ValueCaster::class, 'toInt']);
2✔
176
                }
177
                if ($nativeType->isIdentifiedBy(TypeIdentifier::FLOAT)) {
2✔
178
                    $parameter = $parameter->withCastFn([ValueCaster::class, 'toFloat']);
2✔
179
                }
180
            }
181

182
            $priority = $parameter->getPriority() ?? $internalPriority--;
30✔
183
            $parameters->add($key, $parameter->withPriority($priority));
30✔
184
        }
185

186
        return $parameters;
110✔
187
    }
188

189
    private function addFilterMetadata(Parameter $parameter): Parameter
190
    {
191
        if (!($filterId = $parameter->getFilter())) {
30✔
192
            return $parameter;
12✔
193
        }
194

195
        if (!\is_object($filterId) && !$this->filterLocator->has($filterId)) {
22✔
196
            return $parameter;
×
197
        }
198

199
        $filter = \is_object($filterId) ? $filterId : $this->filterLocator->get($filterId);
22✔
200

201
        if ($filter instanceof ParameterProviderFilterInterface) {
22✔
202
            $parameter = $parameter->withProvider($filter::getParameterProvider());
2✔
203
        }
204

205
        if (!$filter) {
22✔
206
            return $parameter;
×
207
        }
208

209
        if (null === $parameter->getSchema() && $filter instanceof JsonSchemaFilterInterface && $schema = $filter->getSchema($parameter)) {
22✔
210
            $parameter = $parameter->withSchema($schema);
18✔
211
        }
212

213
        if (null === $parameter->getOpenApi() && $filter instanceof OpenApiParameterFilterInterface && ($openApiParameter = $filter->getOpenApiParameters($parameter))) {
22✔
214
            $parameter = $parameter->withOpenApi($openApiParameter);
12✔
215
        }
216

217
        return $parameter;
22✔
218
    }
219

220
    /**
221
     * @param array<string, ApiProperty> $properties
222
     */
223
    private function setDefaults(string $key, Parameter $parameter, string $resourceClass, array $properties, Operation $operation): Parameter
224
    {
225
        if (null === $parameter->getKey()) {
30✔
226
            $parameter = $parameter->withKey($key);
30✔
227
        }
228

229
        $filter = $parameter->getFilter();
30✔
230
        if (\is_string($filter) && $this->filterLocator->has($filter)) {
30✔
231
            $filter = $this->filterLocator->get($filter);
6✔
232
        }
233

234
        if ($filter instanceof SerializerFilterInterface && null === $parameter->getProvider()) {
30✔
235
            $parameter = $parameter->withProvider('api_platform.serializer.filter_parameter_provider');
2✔
236
        }
237
        $currentKey = $key;
30✔
238
        if (null === $parameter->getProperty() && isset($properties[$key])) {
30✔
239
            $parameter = $parameter->withProperty($key);
18✔
240
        }
241

242
        if (null === $parameter->getProperty() && $this->nameConverter && ($nameConvertedKey = $this->nameConverter->normalize($key)) && isset($properties[$nameConvertedKey])) {
30✔
243
            $parameter = $parameter->withProperty($key)->withExtraProperties(['_query_property' => $nameConvertedKey] + $parameter->getExtraProperties());
×
244
            $currentKey = $nameConvertedKey;
×
245
        }
246

247
        if ($this->nameConverter && $property = $parameter->getProperty()) {
30✔
248
            $parameter = $parameter->withProperty($this->nameConverter->normalize($property));
22✔
249
        }
250

251
        if (isset($properties[$currentKey]) && ($eloquentRelation = ($properties[$currentKey]->getExtraProperties()['eloquent_relation'] ?? null)) && isset($eloquentRelation['foreign_key'])) {
30✔
252
            $parameter = $parameter->withExtraProperties(['_query_property' => $eloquentRelation['foreign_key']] + $parameter->getExtraProperties());
×
253
        }
254

255
        $parameter = $this->addFilterMetadata($parameter);
30✔
256

257
        if ($filter instanceof FilterInterface) {
30✔
258
            try {
259
                return $this->getLegacyFilterMetadata($parameter, $operation, $filter);
22✔
260
            } catch (RuntimeException $exception) {
12✔
261
                $this->logger?->alert($exception->getMessage(), ['exception' => $exception]);
12✔
262

263
                return $parameter;
12✔
264
            }
265
        }
266

267
        return $parameter;
12✔
268
    }
269

270
    private function getLegacyFilterMetadata(Parameter $parameter, Operation $operation, FilterInterface $filter): Parameter
271
    {
272
        $description = $filter->getDescription($this->getStateOptionsClass($operation, $operation->getClass()));
22✔
273
        $key = $parameter->getKey();
10✔
274
        if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
10✔
275
            $parameter = $parameter->withSchema($schema);
×
276
        }
277

278
        if (null === $parameter->getProperty() && ($property = $description[$key]['property'] ?? null)) {
10✔
279
            $parameter = $parameter->withProperty($property);
×
280
        }
281

282
        if (null === $parameter->getRequired() && ($required = $description[$key]['required'] ?? null)) {
10✔
283
            $parameter = $parameter->withRequired($required);
×
284
        }
285

286
        if (null === $parameter->getOpenApi() && ($openApi = $description[$key]['openapi'] ?? null) && $openApi instanceof OpenApiParameter) {
10✔
287
            $parameter = $parameter->withOpenApi($openApi);
×
288
        }
289

290
        return $parameter;
10✔
291
    }
292
}
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

© 2025 Coveralls, Inc