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

api-platform / core / 18089958695

29 Sep 2025 07:57AM UTC coverage: 21.569% (-0.2%) from 21.797%
18089958695

push

github

web-flow
fix(openapi): define items type for HydraCollectionBaseSchema hydra:member (#7419)

Co-authored-by: Thibaut Cholley <thibaut.cholley@elsie-sante.fr>

0 of 1 new or added line in 1 file covered. (0.0%)

12063 existing lines in 401 files now uncovered.

11765 of 54545 relevant lines covered (21.57%)

12.57 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
    ) {
UNCOV
56
    }
362✔
57

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

UNCOV
62
        foreach ($resourceMetadataCollection as $i => $resource) {
56✔
UNCOV
63
            $operations = $resource->getOperations();
55✔
64

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

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

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

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

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

UNCOV
90
        return $resourceMetadataCollection;
56✔
91
    }
92

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

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

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

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

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

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

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

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

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

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

UNCOV
151
            $parameter = $this->setDefaults($key, $parameter, $resourceClass, $properties, $operation);
16✔
152
            // We don't do any type cast yet, a query parameter or an header is always a string or a list of strings
UNCOV
153
            if (null === $parameter->getNativeType()) {
16✔
154
                // this forces the type to be only a list
UNCOV
155
                if ('array' === ($parameter->getSchema()['type'] ?? null)) {
14✔
UNCOV
156
                    $parameter = $parameter->withNativeType(Type::list(Type::string()));
1✔
UNCOV
157
                } elseif ('string' === ($parameter->getSchema()['type'] ?? null)) {
14✔
UNCOV
158
                    $parameter = $parameter->withNativeType(Type::string());
6✔
UNCOV
159
                } elseif ('boolean' === ($parameter->getSchema()['type'] ?? null)) {
10✔
UNCOV
160
                    $parameter = $parameter->withNativeType(Type::bool());
3✔
UNCOV
161
                } elseif ('integer' === ($parameter->getSchema()['type'] ?? null)) {
8✔
UNCOV
162
                    $parameter = $parameter->withNativeType(Type::int());
1✔
UNCOV
163
                } elseif ('number' === ($parameter->getSchema()['type'] ?? null)) {
8✔
UNCOV
164
                    $parameter = $parameter->withNativeType(Type::float());
2✔
165
                } else {
UNCOV
166
                    $parameter = $parameter->withNativeType(Type::union(Type::string(), Type::list(Type::string())));
7✔
167
                }
168
            }
169

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

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

UNCOV
186
        return $parameters;
55✔
187
    }
188

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

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

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

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

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

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

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

UNCOV
217
        return $parameter;
11✔
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
    {
UNCOV
225
        if (null === $parameter->getKey()) {
16✔
UNCOV
226
            $parameter = $parameter->withKey($key);
16✔
227
        }
228

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

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

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

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

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

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

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

UNCOV
263
                return $parameter;
6✔
264
            }
265
        }
266

UNCOV
267
        return $parameter;
7✔
268
    }
269

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

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

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

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

UNCOV
290
        return $parameter;
5✔
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