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

api-platform / core / 10508505187

22 Aug 2024 12:55PM UTC coverage: 7.704%. Remained the same
10508505187

push

github

soyuka
Merge 3.4

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

8840 existing lines in 284 files now uncovered.

12477 of 161949 relevant lines covered (7.7%)

22.99 hits per line

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

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

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

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

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

UNCOV
58
            $internalPriority = -1;
34✔
UNCOV
59
            foreach ($operations as $operationName => $operation) {
34✔
UNCOV
60
                $parameters = $operation->getParameters() ?? new Parameters();
34✔
UNCOV
61
                foreach ($parameters as $key => $parameter) {
34✔
UNCOV
62
                    $key = $parameter->getKey() ?? $key;
3✔
UNCOV
63
                    $parameter = $this->setDefaults($key, $parameter, $resourceClass);
3✔
UNCOV
64
                    $priority = $parameter->getPriority() ?? $internalPriority--;
3✔
UNCOV
65
                    $parameters->add($key, $parameter->withPriority($priority));
3✔
66
                }
67

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

UNCOV
73
                if (\count($parameters) > 0) {
34✔
UNCOV
74
                    $operations->add($operationName, $operation->withParameters($parameters));
13✔
75
                }
76
            }
77

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

UNCOV
80
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
34✔
UNCOV
81
                continue;
16✔
82
            }
83

UNCOV
84
            $internalPriority = -1;
31✔
UNCOV
85
            foreach ($graphQlOperations as $operationName => $operation) {
31✔
UNCOV
86
                $parameters = $operation->getParameters() ?? new Parameters();
31✔
UNCOV
87
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
31✔
UNCOV
88
                    $key = $parameter->getKey() ?? $key;
3✔
UNCOV
89
                    $parameter = $this->setDefaults($key, $parameter, $resourceClass);
3✔
UNCOV
90
                    $priority = $parameter->getPriority() ?? $internalPriority--;
3✔
UNCOV
91
                    $parameters->add($key, $parameter->withPriority($priority));
3✔
92
                }
93

UNCOV
94
                $graphQlOperations[$operationName] = $operation->withParameters($parameters);
31✔
95
            }
96

UNCOV
97
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
31✔
98
        }
99

UNCOV
100
        return $resourceMetadataCollection;
42✔
101
    }
102

103
    private function setDefaults(string $key, Parameter $parameter, string $resourceClass): Parameter
104
    {
UNCOV
105
        if (null === $parameter->getKey()) {
3✔
UNCOV
106
            $parameter = $parameter->withKey($key);
3✔
107
        }
108

UNCOV
109
        $filter = $parameter->getFilter();
3✔
UNCOV
110
        if (\is_string($filter) && $this->filterLocator->has($filter)) {
3✔
UNCOV
111
            $filter = $this->filterLocator->get($filter);
3✔
112
        }
113

UNCOV
114
        if ($filter instanceof SerializerFilterInterface && null === $parameter->getProvider()) {
3✔
UNCOV
115
            $parameter = $parameter->withProvider('api_platform.serializer.filter_parameter_provider');
3✔
116
        }
117

118
        // Read filter description to populate the Parameter
UNCOV
119
        $description = $filter instanceof FilterInterface ? $filter->getDescription($resourceClass) : [];
3✔
UNCOV
120
        if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
3✔
121
            $parameter = $parameter->withSchema($schema);
×
122
        }
123

UNCOV
124
        if (null === $parameter->getProperty() && ($property = $description[$key]['property'] ?? null)) {
3✔
UNCOV
125
            $parameter = $parameter->withProperty($property);
3✔
126
        }
127

UNCOV
128
        if (null === $parameter->getRequired() && ($required = $description[$key]['required'] ?? null)) {
3✔
129
            $parameter = $parameter->withRequired($required);
×
130
        }
131

UNCOV
132
        if (null === $parameter->getOpenApi() && ($openApi = $description[$key]['openapi'] ?? null) && $openApi instanceof OpenApiParameter) {
3✔
133
            $parameter = $parameter->withOpenApi($openApi);
×
134
        }
135

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

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

UNCOV
143
        return $parameter;
3✔
144
    }
145

146
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, bool $required = false, ?OpenApiParameter $openApi = null): Parameter
147
    {
UNCOV
148
        $assertions = [];
13✔
149

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

UNCOV
154
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
13✔
UNCOV
155
            $assertions[] = new NotBlank(allowNull: !$required);
8✔
156
        }
157

UNCOV
158
        if (isset($schema['exclusiveMinimum'])) {
13✔
UNCOV
159
            $assertions[] = new GreaterThan(value: $schema['exclusiveMinimum']);
4✔
160
        }
161

UNCOV
162
        if (isset($schema['exclusiveMaximum'])) {
13✔
UNCOV
163
            $assertions[] = new LessThan(value: $schema['exclusiveMaximum']);
4✔
164
        }
165

UNCOV
166
        if (isset($schema['minimum'])) {
13✔
UNCOV
167
            $assertions[] = new GreaterThanOrEqual(value: $schema['minimum']);
4✔
168
        }
169

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

UNCOV
174
        if (isset($schema['pattern'])) {
13✔
UNCOV
175
            $assertions[] = new Regex($schema['pattern']);
4✔
176
        }
177

UNCOV
178
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
13✔
UNCOV
179
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
4✔
180
        }
181

UNCOV
182
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
13✔
UNCOV
183
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
4✔
184
        }
185

UNCOV
186
        if (isset($schema['multipleOf'])) {
13✔
UNCOV
187
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
4✔
188
        }
189

UNCOV
190
        if ($schema['uniqueItems'] ?? false) {
13✔
UNCOV
191
            $assertions[] = new Unique();
4✔
192
        }
193

UNCOV
194
        if (isset($schema['enum'])) {
13✔
UNCOV
195
            $assertions[] = new Choice(choices: $schema['enum']);
6✔
196
        }
197

UNCOV
198
        if (isset($schema['type']) && 'array' === $schema['type']) {
13✔
UNCOV
199
            $assertions[] = new Type(type: 'array');
3✔
200
        }
201

UNCOV
202
        if (!$assertions) {
13✔
UNCOV
203
            return $parameter;
10✔
204
        }
205

UNCOV
206
        if (1 === \count($assertions)) {
9✔
UNCOV
207
            return $parameter->withConstraints($assertions[0]);
9✔
208
        }
209

UNCOV
210
        return $parameter->withConstraints($assertions);
3✔
211
    }
212

213
    private function addFilterValidation(HttpOperation $operation): Parameters
214
    {
UNCOV
215
        $parameters = new Parameters();
15✔
UNCOV
216
        $internalPriority = -1;
15✔
217

UNCOV
218
        foreach ($operation->getFilters() as $filter) {
15✔
UNCOV
219
            if (!$this->filterLocator->has($filter)) {
15✔
UNCOV
220
                continue;
2✔
221
            }
222

UNCOV
223
            $filter = $this->filterLocator->get($filter);
15✔
UNCOV
224
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
15✔
UNCOV
225
                $key = $parameterName;
13✔
UNCOV
226
                $required = $definition['required'] ?? false;
13✔
UNCOV
227
                $schema = $definition['schema'] ?? null;
13✔
228

UNCOV
229
                $openApi = null;
13✔
UNCOV
230
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
13✔
UNCOV
231
                    $openApi = $definition['openapi'];
7✔
232
                }
233

234
                // The query parameter validator forced this, lets maintain BC on filters
UNCOV
235
                if (true === $required && !$openApi) {
13✔
UNCOV
236
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
4✔
237
                }
238

UNCOV
239
                $parameters->add($key, $this->addSchemaValidation(
13✔
240
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
UNCOV
241
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
13✔
UNCOV
242
                    $schema,
13✔
UNCOV
243
                    $required,
13✔
UNCOV
244
                    $openApi
13✔
UNCOV
245
                ));
13✔
246
            }
247
        }
248

UNCOV
249
        return $parameters;
15✔
250
    }
251
}
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