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

api-platform / core / 10884379752

16 Sep 2024 01:01PM UTC coverage: 7.281% (-0.4%) from 7.672%
10884379752

push

github

soyuka
Merge 3.4

0 of 100 new or added lines in 7 files covered. (0.0%)

5332 existing lines in 181 files now uncovered.

11994 of 164725 relevant lines covered (7.28%)

9.52 hits per line

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

94.48
/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\FilterInterface;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
20
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
21
use ApiPlatform\Metadata\Parameter;
22
use ApiPlatform\Metadata\Parameters;
23
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
24
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
25
use ApiPlatform\Metadata\QueryParameter;
26
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
27
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
28
use ApiPlatform\Serializer\Filter\FilterInterface as SerializerFilterInterface;
29
use Psr\Container\ContainerInterface;
30
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
31
use Symfony\Component\Validator\Constraints\Choice;
32
use Symfony\Component\Validator\Constraints\Count;
33
use Symfony\Component\Validator\Constraints\DivisibleBy;
34
use Symfony\Component\Validator\Constraints\GreaterThan;
35
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
36
use Symfony\Component\Validator\Constraints\Length;
37
use Symfony\Component\Validator\Constraints\LessThan;
38
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
39
use Symfony\Component\Validator\Constraints\NotBlank;
40
use Symfony\Component\Validator\Constraints\NotNull;
41
use Symfony\Component\Validator\Constraints\Regex;
42
use Symfony\Component\Validator\Constraints\Type;
43
use Symfony\Component\Validator\Constraints\Unique;
44
use Symfony\Component\Validator\Validator\ValidatorInterface;
45

46
/**
47
 * Prepares Parameters documentation by reading its filter details and declaring an OpenApi parameter.
48
 *
49
 * @experimental
50
 */
51
final class ParameterResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
52
{
53
    public function __construct(
54
        private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory,
55
        private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory,
56
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
57
        private readonly ?ContainerInterface $filterLocator = null,
58
        private readonly ?NameConverterInterface $nameConverter = null,
59
    ) {
UNCOV
60
    }
843✔
61

62
    public function create(string $resourceClass): ResourceMetadataCollection
63
    {
UNCOV
64
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
32✔
65

UNCOV
66
        $propertyNames = [];
32✔
UNCOV
67
        $properties = [];
32✔
UNCOV
68
        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $i => $property) {
32✔
UNCOV
69
            $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property);
32✔
UNCOV
70
            if ('author' === $property) {
32✔
71
            }
UNCOV
72
            if ($propertyMetadata->isReadable()) {
32✔
UNCOV
73
                $propertyNames[] = $property;
32✔
UNCOV
74
                $properties[$property] = $propertyMetadata;
32✔
75
            }
76
        }
77

UNCOV
78
        foreach ($resourceMetadataCollection as $i => $resource) {
32✔
UNCOV
79
            $operations = $resource->getOperations();
30✔
80

UNCOV
81
            $internalPriority = -1;
30✔
UNCOV
82
            foreach ($operations as $operationName => $operation) {
30✔
UNCOV
83
                $parameters = $operation->getParameters() ?? new Parameters();
30✔
UNCOV
84
                foreach ($parameters as $key => $parameter) {
30✔
UNCOV
85
                    if (':property' === $key) {
1✔
UNCOV
86
                        foreach ($propertyNames as $property) {
1✔
UNCOV
87
                            $converted = $this->nameConverter?->denormalize($property) ?? $property;
1✔
UNCOV
88
                            $propertyParameter = $this->setDefaults($converted, $parameter, $resourceClass, $properties);
1✔
UNCOV
89
                            $priority = $propertyParameter->getPriority() ?? $internalPriority--;
1✔
UNCOV
90
                            $parameters->add($converted, $this->addFilterMetadata($propertyParameter->withPriority($priority)->withKey($converted)));
1✔
91
                        }
92

UNCOV
93
                        $parameters->remove($key, $parameter::class);
1✔
UNCOV
94
                        continue;
1✔
95
                    }
96

UNCOV
97
                    $key = $parameter->getKey() ?? $key;
1✔
98

UNCOV
99
                    if (str_contains($key, ':property')) {
1✔
UNCOV
100
                        $p = [];
1✔
UNCOV
101
                        foreach ($propertyNames as $prop) {
1✔
UNCOV
102
                            $p[$this->nameConverter?->denormalize($prop) ?? $prop] = $prop;
1✔
103
                        }
104

UNCOV
105
                        $parameter = $parameter->withExtraProperties(($parameter->getExtraProperties() ?? []) + ['_properties' => $p]);
1✔
106
                    }
107

UNCOV
108
                    $parameter = $this->setDefaults($key, $parameter, $resourceClass, $properties);
1✔
UNCOV
109
                    $priority = $parameter->getPriority() ?? $internalPriority--;
1✔
UNCOV
110
                    $parameters->add($key, $this->addFilterMetadata($parameter->withPriority($priority)));
1✔
111
                }
112

113
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
UNCOV
114
                if ($operation->getFilters() && 0 === $parameters->count()) {
30✔
UNCOV
115
                    $parameters = $this->addFilterValidation($operation);
11✔
116
                }
117

UNCOV
118
                if (\count($parameters) > 0) {
30✔
UNCOV
119
                    $operations->add($operationName, $operation->withParameters($parameters));
11✔
120
                }
121
            }
122

UNCOV
123
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
30✔
124

UNCOV
125
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
30✔
UNCOV
126
                continue;
12✔
127
            }
128

UNCOV
129
            $internalPriority = -1;
27✔
UNCOV
130
            foreach ($graphQlOperations as $operationName => $operation) {
27✔
UNCOV
131
                $parameters = $operation->getParameters() ?? new Parameters();
27✔
UNCOV
132
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
27✔
UNCOV
133
                    $key = $parameter->getKey() ?? $key;
1✔
UNCOV
134
                    $parameter = $this->setDefaults($key, $parameter, $resourceClass, $properties);
1✔
UNCOV
135
                    $priority = $parameter->getPriority() ?? $internalPriority--;
1✔
UNCOV
136
                    $parameters->add($key, $parameter->withPriority($priority));
1✔
137
                }
138

UNCOV
139
                $graphQlOperations[$operationName] = $operation->withParameters($parameters);
27✔
140
            }
141

UNCOV
142
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
27✔
143
        }
144

UNCOV
145
        return $resourceMetadataCollection;
32✔
146
    }
147

148
    private function addFilterMetadata(Parameter $parameter): Parameter
149
    {
UNCOV
150
        if (!($filterId = $parameter->getFilter())) {
1✔
UNCOV
151
            return $parameter;
1✔
152
        }
153

UNCOV
154
        $filter = \is_object($filterId) ? $filterId : $this->filterLocator->get($filterId);
1✔
155

UNCOV
156
        if (!$filter) {
1✔
157
            return $parameter;
×
158
        }
159

UNCOV
160
        if (null === $parameter->getSchema() && $filter instanceof JsonSchemaFilterInterface) {
1✔
UNCOV
161
            if ($schema = $filter->getSchema($parameter)) {
1✔
UNCOV
162
                $parameter = $parameter->withSchema($schema);
1✔
163
            }
164
        }
165

UNCOV
166
        if (null === $parameter->getOpenApi() && $filter instanceof OpenApiParameterFilterInterface) {
1✔
UNCOV
167
            if ($openApiParameter = $filter->getOpenApiParameters($parameter)) {
1✔
UNCOV
168
                $parameter = $parameter->withOpenApi($openApiParameter);
1✔
169
            }
170
        }
171

UNCOV
172
        return $parameter;
1✔
173
    }
174

175
    /**
176
     * @param array<string, ApiProperty> $properties
177
     */
178
    private function setDefaults(string $key, Parameter $parameter, string $resourceClass, array $properties): Parameter
179
    {
UNCOV
180
        if (null === $parameter->getKey()) {
1✔
UNCOV
181
            $parameter = $parameter->withKey($key);
1✔
182
        }
183

UNCOV
184
        $filter = $parameter->getFilter();
1✔
UNCOV
185
        if (\is_string($filter) && $this->filterLocator->has($filter)) {
1✔
UNCOV
186
            $filter = $this->filterLocator->get($filter);
1✔
187
        }
188

UNCOV
189
        if ($filter instanceof SerializerFilterInterface && null === $parameter->getProvider()) {
1✔
UNCOV
190
            $parameter = $parameter->withProvider('api_platform.serializer.filter_parameter_provider');
1✔
191
        }
192

193
        // Read filter description to populate the Parameter
UNCOV
194
        $description = $filter instanceof FilterInterface ? $filter->getDescription($resourceClass) : [];
1✔
UNCOV
195
        if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
1✔
196
            $parameter = $parameter->withSchema($schema);
×
197
        }
198

UNCOV
199
        if (null === $parameter->getProperty() && ($property = $description[$key]['property'] ?? null)) {
1✔
UNCOV
200
            $parameter = $parameter->withProperty($property);
1✔
201
        }
202

UNCOV
203
        $currentKey = $key;
1✔
UNCOV
204
        if (null === $parameter->getProperty() && isset($properties[$key])) {
1✔
UNCOV
205
            $parameter = $parameter->withProperty($key);
1✔
206
        }
207

UNCOV
208
        if (null === $parameter->getProperty() && $this->nameConverter && ($nameConvertedKey = $this->nameConverter->normalize($key)) && isset($properties[$nameConvertedKey])) {
1✔
209
            $parameter = $parameter->withProperty($key)->withExtraProperties(['_query_property' => $nameConvertedKey] + $parameter->getExtraProperties());
×
210
            $currentKey = $nameConvertedKey;
×
211
        }
212

UNCOV
213
        if (isset($properties[$currentKey]) && ($eloquentRelation = ($properties[$currentKey]->getExtraProperties()['eloquent_relation'] ?? null)) && isset($eloquentRelation['foreign_key'])) {
1✔
214
            $parameter = $parameter->withExtraProperties(['_query_property' => $eloquentRelation['foreign_key']] + $parameter->getExtraProperties());
×
215
        }
216

UNCOV
217
        if (null === $parameter->getRequired() && ($required = $description[$key]['required'] ?? null)) {
1✔
218
            $parameter = $parameter->withRequired($required);
×
219
        }
220

UNCOV
221
        if (null === $parameter->getOpenApi() && ($openApi = $description[$key]['openapi'] ?? null) && $openApi instanceof OpenApiParameter) {
1✔
222
            $parameter = $parameter->withOpenApi($openApi);
×
223
        }
224

UNCOV
225
        $schema = $parameter->getSchema() ?? (($openApi = $parameter->getOpenApi()) ? $openApi->getSchema() : null);
1✔
226

227
        // Only add validation if the Symfony Validator is installed
UNCOV
228
        if (interface_exists(ValidatorInterface::class) && !$parameter->getConstraints()) {
1✔
UNCOV
229
            $parameter = $this->addSchemaValidation($parameter, $schema, $parameter->getRequired() ?? $description['required'] ?? false, $parameter->getOpenApi() ?: null);
1✔
230
        }
231

UNCOV
232
        return $parameter;
1✔
233
    }
234

235
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, bool $required = false, ?OpenApiParameter $openApi = null): Parameter
236
    {
UNCOV
237
        $assertions = [];
11✔
238

UNCOV
239
        if ($required && false !== ($allowEmptyValue = $openApi?->getAllowEmptyValue())) {
11✔
UNCOV
240
            $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
2✔
241
        }
242

UNCOV
243
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
11✔
UNCOV
244
            $assertions[] = new NotBlank(allowNull: !$required);
6✔
245
        }
246

UNCOV
247
        if (isset($schema['exclusiveMinimum'])) {
11✔
UNCOV
248
            $assertions[] = new GreaterThan(value: $schema['exclusiveMinimum']);
2✔
249
        }
250

UNCOV
251
        if (isset($schema['exclusiveMaximum'])) {
11✔
UNCOV
252
            $assertions[] = new LessThan(value: $schema['exclusiveMaximum']);
2✔
253
        }
254

UNCOV
255
        if (isset($schema['minimum'])) {
11✔
UNCOV
256
            $assertions[] = new GreaterThanOrEqual(value: $schema['minimum']);
2✔
257
        }
258

UNCOV
259
        if (isset($schema['maximum'])) {
11✔
UNCOV
260
            $assertions[] = new LessThanOrEqual(value: $schema['maximum']);
2✔
261
        }
262

UNCOV
263
        if (isset($schema['pattern'])) {
11✔
UNCOV
264
            $assertions[] = new Regex($schema['pattern']);
2✔
265
        }
266

UNCOV
267
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
11✔
UNCOV
268
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
2✔
269
        }
270

UNCOV
271
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
11✔
UNCOV
272
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
2✔
273
        }
274

UNCOV
275
        if (isset($schema['multipleOf'])) {
11✔
UNCOV
276
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
2✔
277
        }
278

UNCOV
279
        if ($schema['uniqueItems'] ?? false) {
11✔
UNCOV
280
            $assertions[] = new Unique();
2✔
281
        }
282

UNCOV
283
        if (isset($schema['enum'])) {
11✔
UNCOV
284
            $assertions[] = new Choice(choices: $schema['enum']);
4✔
285
        }
286

UNCOV
287
        if (isset($schema['type']) && 'array' === $schema['type']) {
11✔
UNCOV
288
            $assertions[] = new Type(type: 'array');
1✔
289
        }
290

UNCOV
291
        if (!$assertions) {
11✔
UNCOV
292
            return $parameter;
8✔
293
        }
294

UNCOV
295
        if (1 === \count($assertions)) {
7✔
UNCOV
296
            return $parameter->withConstraints($assertions[0]);
7✔
297
        }
298

UNCOV
299
        return $parameter->withConstraints($assertions);
1✔
300
    }
301

302
    private function addFilterValidation(HttpOperation $operation): Parameters
303
    {
UNCOV
304
        $parameters = new Parameters();
11✔
UNCOV
305
        $internalPriority = -1;
11✔
306

UNCOV
307
        foreach ($operation->getFilters() as $filter) {
11✔
UNCOV
308
            if (!$this->filterLocator->has($filter)) {
11✔
UNCOV
309
                continue;
×
310
            }
311

UNCOV
312
            $filter = $this->filterLocator->get($filter);
11✔
UNCOV
313
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
11✔
UNCOV
314
                $key = $parameterName;
11✔
UNCOV
315
                $required = $definition['required'] ?? false;
11✔
UNCOV
316
                $schema = $definition['schema'] ?? null;
11✔
317

UNCOV
318
                $openApi = null;
11✔
UNCOV
319
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
11✔
UNCOV
320
                    $openApi = $definition['openapi'];
5✔
321
                }
322

323
                // The query parameter validator forced this, lets maintain BC on filters
UNCOV
324
                if (true === $required && !$openApi) {
11✔
UNCOV
325
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
2✔
326
                }
327

UNCOV
328
                $parameters->add($key, $this->addSchemaValidation(
11✔
329
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
UNCOV
330
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
11✔
UNCOV
331
                    $schema,
11✔
UNCOV
332
                    $required,
11✔
UNCOV
333
                    $openApi
11✔
UNCOV
334
                ));
11✔
335
            }
336
        }
337

UNCOV
338
        return $parameters;
11✔
339
    }
340
}
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