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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

97.14
/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
    }
978✔
50

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

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

UNCOV
58
            foreach ($operations as $operationName => $operation) {
65✔
UNCOV
59
                $parameters = $operation->getParameters() ?? new Parameters();
65✔
UNCOV
60
                foreach ($parameters as $key => $parameter) {
65✔
UNCOV
61
                    $parameters->add($key, $this->addSchemaValidation($parameter));
15✔
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()) {
65✔
UNCOV
66
                    $parameters = $this->addFilterValidation($operation);
13✔
67
                }
68

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

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

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

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

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

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

UNCOV
94
        return $resourceMetadataCollection;
69✔
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()) {
26✔
UNCOV
100
            return $parameter;
3✔
101
        }
102

UNCOV
103
        $schema ??= $parameter->getSchema();
25✔
UNCOV
104
        $required ??= $parameter->getRequired() ?? false;
25✔
UNCOV
105
        $openApi ??= $parameter->getOpenApi();
25✔
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)) {
25✔
UNCOV
110
            $openApi = $openApi[0];
5✔
UNCOV
111
        } elseif (false === $openApi) {
24✔
UNCOV
112
            $openApi = null;
11✔
113
        }
114

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
233
                $parameters->add($key, $this->addSchemaValidation(
12✔
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),
12✔
UNCOV
236
                    $schema,
12✔
UNCOV
237
                    $required,
12✔
UNCOV
238
                    $openApi
12✔
UNCOV
239
                ));
12✔
240
            }
241
        }
242

UNCOV
243
        return $parameters;
13✔
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