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

api-platform / core / 10729306835

05 Sep 2024 10:46PM UTC coverage: 7.655% (-0.01%) from 7.665%
10729306835

push

github

web-flow
Merge pull request #6586 from soyuka/merge-342

Merge 3.4

0 of 54 new or added lines in 12 files covered. (0.0%)

8760 existing lines in 277 files now uncovered.

12505 of 163357 relevant lines covered (7.66%)

22.84 hits per line

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

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

41
/**
42
 * Prepares Parameters documentation by reading its filter details and declaring an OpenApi parameter.
43
 *
44
 * @experimental
45
 */
46
final class ParameterResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
47
{
48
    public function __construct(private readonly PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null, private readonly ?ContainerInterface $filterLocator = null)
49
    {
UNCOV
50
    }
2,248✔
51

52
    public function create(string $resourceClass): ResourceMetadataCollection
53
    {
UNCOV
54
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
42✔
55

UNCOV
56
        foreach ($resourceMetadataCollection as $i => $resource) {
42✔
UNCOV
57
            $operations = $resource->getOperations();
34✔
58

UNCOV
59
            $internalPriority = -1;
34✔
UNCOV
60
            foreach ($operations as $operationName => $operation) {
34✔
UNCOV
61
                $parameters = $operation->getParameters() ?? new Parameters();
34✔
UNCOV
62
                foreach ($parameters as $key => $parameter) {
34✔
UNCOV
63
                    if (':property' === $key) {
3✔
UNCOV
64
                        foreach ($this->propertyNameCollectionFactory->create($resourceClass) as $property) {
3✔
UNCOV
65
                            $parameter = $this->setDefaults($property, $parameter, $resourceClass);
3✔
UNCOV
66
                            $priority = $parameter->getPriority() ?? $internalPriority--;
3✔
UNCOV
67
                            $parameters->add($property, $parameter->withPriority($priority)->withProperty($property)->withKey($property));
3✔
68
                        }
69

UNCOV
70
                        $parameters->remove($key, $parameter::class);
3✔
UNCOV
71
                        continue;
3✔
72
                    }
73

UNCOV
74
                    $key = $parameter->getKey() ?? $key;
3✔
UNCOV
75
                    $parameter = $this->setDefaults($key, $parameter, $resourceClass);
3✔
UNCOV
76
                    $priority = $parameter->getPriority() ?? $internalPriority--;
3✔
UNCOV
77
                    $parameters->add($key, $parameter->withPriority($priority));
3✔
78
                }
79

80
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
UNCOV
81
                if ($operation->getFilters() && 0 === $parameters->count()) {
34✔
UNCOV
82
                    $parameters = $this->addFilterValidation($operation);
15✔
83
                }
84

UNCOV
85
                if (\count($parameters) > 0) {
34✔
UNCOV
86
                    $operations->add($operationName, $operation->withParameters($parameters));
13✔
87
                }
88
            }
89

UNCOV
90
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
34✔
91

UNCOV
92
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
34✔
UNCOV
93
                continue;
16✔
94
            }
95

UNCOV
96
            $internalPriority = -1;
31✔
UNCOV
97
            foreach ($graphQlOperations as $operationName => $operation) {
31✔
UNCOV
98
                $parameters = $operation->getParameters() ?? new Parameters();
31✔
UNCOV
99
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
31✔
UNCOV
100
                    $key = $parameter->getKey() ?? $key;
3✔
UNCOV
101
                    $parameter = $this->setDefaults($key, $parameter, $resourceClass);
3✔
UNCOV
102
                    $priority = $parameter->getPriority() ?? $internalPriority--;
3✔
UNCOV
103
                    $parameters->add($key, $parameter->withPriority($priority));
3✔
104
                }
105

UNCOV
106
                $graphQlOperations[$operationName] = $operation->withParameters($parameters);
31✔
107
            }
108

UNCOV
109
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
31✔
110
        }
111

UNCOV
112
        return $resourceMetadataCollection;
42✔
113
    }
114

115
    private function setDefaults(string $key, Parameter $parameter, string $resourceClass): Parameter
116
    {
UNCOV
117
        if (null === $parameter->getKey()) {
3✔
UNCOV
118
            $parameter = $parameter->withKey($key);
3✔
119
        }
120

UNCOV
121
        $filter = $parameter->getFilter();
3✔
UNCOV
122
        if (\is_string($filter) && $this->filterLocator->has($filter)) {
3✔
UNCOV
123
            $filter = $this->filterLocator->get($filter);
3✔
124
        }
125

UNCOV
126
        if ($filter instanceof SerializerFilterInterface && null === $parameter->getProvider()) {
3✔
UNCOV
127
            $parameter = $parameter->withProvider('api_platform.serializer.filter_parameter_provider');
3✔
128
        }
129

130
        // Read filter description to populate the Parameter
UNCOV
131
        $description = $filter instanceof FilterInterface ? $filter->getDescription($resourceClass) : [];
3✔
UNCOV
132
        if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
3✔
133
            $parameter = $parameter->withSchema($schema);
×
134
        }
135

UNCOV
136
        if (null === $parameter->getProperty() && ($property = $description[$key]['property'] ?? null)) {
3✔
UNCOV
137
            $parameter = $parameter->withProperty($property);
3✔
138
        }
139

UNCOV
140
        if (null === $parameter->getRequired() && ($required = $description[$key]['required'] ?? null)) {
3✔
141
            $parameter = $parameter->withRequired($required);
×
142
        }
143

UNCOV
144
        if (null === $parameter->getOpenApi() && ($openApi = $description[$key]['openapi'] ?? null) && $openApi instanceof OpenApiParameter) {
3✔
145
            $parameter = $parameter->withOpenApi($openApi);
×
146
        }
147

UNCOV
148
        $schema = $parameter->getSchema() ?? (($openApi = $parameter->getOpenApi()) ? $openApi->getSchema() : null);
3✔
149

150
        // Only add validation if the Symfony Validator is installed
UNCOV
151
        if (interface_exists(ValidatorInterface::class) && !$parameter->getConstraints()) {
3✔
UNCOV
152
            $parameter = $this->addSchemaValidation($parameter, $schema, $parameter->getRequired() ?? $description['required'] ?? false, $parameter->getOpenApi() ?: null);
3✔
153
        }
154

UNCOV
155
        return $parameter;
3✔
156
    }
157

158
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, bool $required = false, ?OpenApiParameter $openApi = null): Parameter
159
    {
UNCOV
160
        $assertions = [];
13✔
161

UNCOV
162
        if ($required && false !== ($allowEmptyValue = $openApi?->getAllowEmptyValue())) {
13✔
UNCOV
163
            $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
4✔
164
        }
165

UNCOV
166
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
13✔
UNCOV
167
            $assertions[] = new NotBlank(allowNull: !$required);
8✔
168
        }
169

UNCOV
170
        if (isset($schema['exclusiveMinimum'])) {
13✔
UNCOV
171
            $assertions[] = new GreaterThan(value: $schema['exclusiveMinimum']);
4✔
172
        }
173

UNCOV
174
        if (isset($schema['exclusiveMaximum'])) {
13✔
UNCOV
175
            $assertions[] = new LessThan(value: $schema['exclusiveMaximum']);
4✔
176
        }
177

UNCOV
178
        if (isset($schema['minimum'])) {
13✔
UNCOV
179
            $assertions[] = new GreaterThanOrEqual(value: $schema['minimum']);
4✔
180
        }
181

UNCOV
182
        if (isset($schema['maximum'])) {
13✔
UNCOV
183
            $assertions[] = new LessThanOrEqual(value: $schema['maximum']);
4✔
184
        }
185

UNCOV
186
        if (isset($schema['pattern'])) {
13✔
UNCOV
187
            $assertions[] = new Regex($schema['pattern']);
4✔
188
        }
189

UNCOV
190
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
13✔
UNCOV
191
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
4✔
192
        }
193

UNCOV
194
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
13✔
UNCOV
195
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
4✔
196
        }
197

UNCOV
198
        if (isset($schema['multipleOf'])) {
13✔
UNCOV
199
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
4✔
200
        }
201

UNCOV
202
        if ($schema['uniqueItems'] ?? false) {
13✔
UNCOV
203
            $assertions[] = new Unique();
4✔
204
        }
205

UNCOV
206
        if (isset($schema['enum'])) {
13✔
UNCOV
207
            $assertions[] = new Choice(choices: $schema['enum']);
6✔
208
        }
209

UNCOV
210
        if (isset($schema['type']) && 'array' === $schema['type']) {
13✔
UNCOV
211
            $assertions[] = new Type(type: 'array');
3✔
212
        }
213

UNCOV
214
        if (!$assertions) {
13✔
UNCOV
215
            return $parameter;
10✔
216
        }
217

UNCOV
218
        if (1 === \count($assertions)) {
9✔
UNCOV
219
            return $parameter->withConstraints($assertions[0]);
9✔
220
        }
221

UNCOV
222
        return $parameter->withConstraints($assertions);
3✔
223
    }
224

225
    private function addFilterValidation(HttpOperation $operation): Parameters
226
    {
UNCOV
227
        $parameters = new Parameters();
15✔
UNCOV
228
        $internalPriority = -1;
15✔
229

UNCOV
230
        foreach ($operation->getFilters() as $filter) {
15✔
UNCOV
231
            if (!$this->filterLocator->has($filter)) {
15✔
UNCOV
232
                continue;
2✔
233
            }
234

UNCOV
235
            $filter = $this->filterLocator->get($filter);
15✔
UNCOV
236
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
15✔
UNCOV
237
                $key = $parameterName;
13✔
UNCOV
238
                $required = $definition['required'] ?? false;
13✔
UNCOV
239
                $schema = $definition['schema'] ?? null;
13✔
240

UNCOV
241
                $openApi = null;
13✔
UNCOV
242
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
13✔
UNCOV
243
                    $openApi = $definition['openapi'];
7✔
244
                }
245

246
                // The query parameter validator forced this, lets maintain BC on filters
UNCOV
247
                if (true === $required && !$openApi) {
13✔
UNCOV
248
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
4✔
249
                }
250

UNCOV
251
                $parameters->add($key, $this->addSchemaValidation(
13✔
252
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
UNCOV
253
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
13✔
UNCOV
254
                    $schema,
13✔
UNCOV
255
                    $required,
13✔
UNCOV
256
                    $openApi
13✔
UNCOV
257
                ));
13✔
258
            }
259
        }
260

UNCOV
261
        return $parameters;
15✔
262
    }
263
}
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