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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

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

11394 existing lines in 372 files now uncovered.

0 of 51334 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.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\Validator\Metadata\Resource\Factory;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Parameter;
18
use ApiPlatform\Metadata\Parameters;
19
use ApiPlatform\Metadata\QueryParameter;
20
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
21
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
22
use ApiPlatform\OpenApi\Model\Parameter as OpenApiParameter;
23
use Psr\Container\ContainerInterface;
24
use Symfony\Component\TypeInfo\Type\CollectionType;
25
use Symfony\Component\TypeInfo\Type\UnionType;
26
use Symfony\Component\Validator\Constraints\All;
27
use Symfony\Component\Validator\Constraints\Choice;
28
use Symfony\Component\Validator\Constraints\Collection;
29
use Symfony\Component\Validator\Constraints\Count;
30
use Symfony\Component\Validator\Constraints\DivisibleBy;
31
use Symfony\Component\Validator\Constraints\GreaterThan;
32
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
33
use Symfony\Component\Validator\Constraints\Length;
34
use Symfony\Component\Validator\Constraints\LessThan;
35
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
36
use Symfony\Component\Validator\Constraints\NotBlank;
37
use Symfony\Component\Validator\Constraints\NotNull;
38
use Symfony\Component\Validator\Constraints\Range;
39
use Symfony\Component\Validator\Constraints\Regex;
40
use Symfony\Component\Validator\Constraints\Type;
41
use Symfony\Component\Validator\Constraints\Unique;
42

43
final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
44
{
45
    public function __construct(
46
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
47
        private readonly ?ContainerInterface $filterLocator = null,
48
    ) {
UNCOV
49
    }
×
50

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

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

UNCOV
58
            foreach ($operations as $operationName => $operation) {
×
UNCOV
59
                $parameters = $operation->getParameters() ?? new Parameters();
×
UNCOV
60
                foreach ($parameters as $key => $parameter) {
×
UNCOV
61
                    $parameters->add($key, $this->addSchemaValidation($parameter));
×
62
                }
63

64
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
UNCOV
65
                if ($operation->getFilters() && 0 === $parameters->count()) {
×
UNCOV
66
                    $parameters = $this->addFilterValidation($operation);
×
67
                }
68

UNCOV
69
                if (\count($parameters) > 0) {
×
UNCOV
70
                    $operations->add($operationName, $operation->withParameters($parameters));
×
71
                }
72
            }
73

UNCOV
74
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
×
75

UNCOV
76
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
×
UNCOV
77
                continue;
×
78
            }
79

UNCOV
80
            foreach ($graphQlOperations as $operationName => $operation) {
×
UNCOV
81
                $parameters = $operation->getParameters() ?? new Parameters();
×
UNCOV
82
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
×
UNCOV
83
                    $parameters->add($key, $this->addSchemaValidation($parameter));
×
84
                }
85

UNCOV
86
                if (\count($parameters) > 0) {
×
UNCOV
87
                    $graphQlOperations[$operationName] = $operation->withParameters($parameters);
×
88
                }
89
            }
90

UNCOV
91
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
×
92
        }
93

UNCOV
94
        return $resourceMetadataCollection;
×
95
    }
96

97
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): Parameter
98
    {
UNCOV
99
        if (null !== $parameter->getConstraints()) {
×
UNCOV
100
            return $parameter;
×
101
        }
102

UNCOV
103
        $schema ??= $parameter->getSchema();
×
UNCOV
104
        $required ??= $parameter->getRequired() ?? false;
×
UNCOV
105
        $openApi ??= $parameter->getOpenApi();
×
106

107
        // When it's an array of openapi parameters take the first one as it's probably just a variant of the query parameter,
108
        // only getAllowEmptyValue is used here anyways
UNCOV
109
        if (\is_array($openApi)) {
×
UNCOV
110
            $openApi = $openApi[0];
×
UNCOV
111
        } elseif (false === $openApi) {
×
UNCOV
112
            $openApi = null;
×
113
        }
114

UNCOV
115
        $assertions = [];
×
UNCOV
116
        $allowEmptyValue = $openApi?->getAllowEmptyValue();
×
UNCOV
117
        if (false === ($allowEmptyValue ?? $openApi?->getAllowEmptyValue())) {
×
UNCOV
118
            $assertions[] = new NotBlank(allowNull: !$required);
×
119
        }
120

UNCOV
121
        $minimum = $schema['exclusiveMinimum'] ?? $schema['minimum'] ?? null;
×
UNCOV
122
        $exclusiveMinimum = isset($schema['exclusiveMinimum']);
×
UNCOV
123
        $maximum = $schema['exclusiveMaximum'] ?? $schema['maximum'] ?? null;
×
UNCOV
124
        $exclusiveMaximum = isset($schema['exclusiveMaximum']);
×
125

UNCOV
126
        if ($minimum && $maximum) {
×
UNCOV
127
            if (!$exclusiveMinimum && !$exclusiveMaximum) {
×
UNCOV
128
                $assertions[] = new Range(min: $minimum, max: $maximum);
×
129
            } else {
UNCOV
130
                $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum);
×
UNCOV
131
                $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum);
×
132
            }
UNCOV
133
        } elseif ($minimum) {
×
134
            $assertions[] = $exclusiveMinimum ? new GreaterThan(value: $minimum) : new GreaterThanOrEqual(value: $minimum);
×
UNCOV
135
        } elseif ($maximum) {
×
136
            $assertions[] = $exclusiveMaximum ? new LessThan(value: $maximum) : new LessThanOrEqual(value: $maximum);
×
137
        }
138

UNCOV
139
        if (isset($schema['pattern'])) {
×
UNCOV
140
            $assertions[] = new Regex('#'.$schema['pattern'].'#');
×
141
        }
142

UNCOV
143
        if (isset($schema['maxLength']) || isset($schema['minLength'])) {
×
UNCOV
144
            $assertions[] = new Length(min: $schema['minLength'] ?? null, max: $schema['maxLength'] ?? null);
×
145
        }
146

UNCOV
147
        if (isset($schema['multipleOf'])) {
×
UNCOV
148
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
×
149
        }
150

UNCOV
151
        if (isset($schema['enum'])) {
×
UNCOV
152
            $assertions[] = new Choice(choices: $schema['enum']);
×
153
        }
154

UNCOV
155
        if ($properties = $parameter->getExtraProperties()['_properties'] ?? []) {
×
UNCOV
156
            $fields = [];
×
UNCOV
157
            foreach ($properties as $propertyName) {
×
UNCOV
158
                $fields[$propertyName] = $assertions;
×
159
            }
160

UNCOV
161
            return $parameter->withConstraints(new Collection(fields: $fields, allowMissingFields: true));
×
162
        }
163

UNCOV
164
        $isCollectionType = fn ($t) => $t instanceof CollectionType;
×
UNCOV
165
        $isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
×
166

167
        // type-info 7.2
UNCOV
168
        if (!$isCollection && $parameter->getNativeType() instanceof UnionType) {
×
169
            foreach ($parameter->getNativeType()->getTypes() as $t) {
×
170
                if ($isCollection = $t->isSatisfiedBy($isCollectionType)) {
×
171
                    break;
×
172
                }
173
            }
174
        }
175

UNCOV
176
        if ($isCollection) {
×
UNCOV
177
            $assertions = $assertions ? [new All($assertions)] : [];
×
178
        }
179

UNCOV
180
        if ($required && false !== $allowEmptyValue) {
×
UNCOV
181
            $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
×
182
        }
183

UNCOV
184
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
×
UNCOV
185
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
×
186
        }
187

UNCOV
188
        if ($schema['uniqueItems'] ?? false) {
×
UNCOV
189
            $assertions[] = new Unique();
×
190
        }
191

UNCOV
192
        if (isset($schema['type']) && 'array' === $schema['type']) {
×
UNCOV
193
            $assertions[] = new Type(type: 'array');
×
194
        }
195

UNCOV
196
        if (!$assertions) {
×
UNCOV
197
            return $parameter;
×
198
        }
199

UNCOV
200
        if (1 === \count($assertions)) {
×
UNCOV
201
            return $parameter->withConstraints($assertions[0]);
×
202
        }
203

UNCOV
204
        return $parameter->withConstraints($assertions);
×
205
    }
206

207
    private function addFilterValidation(HttpOperation $operation): Parameters
208
    {
UNCOV
209
        $parameters = new Parameters();
×
UNCOV
210
        $internalPriority = -1;
×
211

UNCOV
212
        foreach ($operation->getFilters() as $filter) {
×
UNCOV
213
            if (!$this->filterLocator->has($filter)) {
×
214
                continue;
×
215
            }
216

UNCOV
217
            $filter = $this->filterLocator->get($filter);
×
UNCOV
218
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
×
UNCOV
219
                $key = $parameterName;
×
UNCOV
220
                $required = $definition['required'] ?? false;
×
UNCOV
221
                $schema = $definition['schema'] ?? null;
×
222

UNCOV
223
                $openApi = null;
×
UNCOV
224
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
×
UNCOV
225
                    $openApi = $definition['openapi'];
×
226
                }
227

228
                // The query parameter validator forced this, lets maintain BC on filters
UNCOV
229
                if (true === $required && !$openApi) {
×
230
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
×
231
                }
232

UNCOV
233
                $parameters->add($key, $this->addSchemaValidation(
×
234
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
UNCOV
235
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
×
UNCOV
236
                    $schema,
×
UNCOV
237
                    $required,
×
UNCOV
238
                    $openApi
×
UNCOV
239
                ));
×
240
            }
241
        }
242

UNCOV
243
        return $parameters;
×
244
    }
245
}
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