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

api-platform / core / 14922365773

09 May 2025 05:50AM UTC coverage: 6.886% (-1.6%) from 8.457%
14922365773

Pull #7134

github

web-flow
Merge ebf237e3f into 613bb5b75
Pull Request #7134: refactor(metadata): type parameters to list<string>|string

46 of 132 new or added lines in 15 files covered. (34.85%)

1 existing line in 1 file now uncovered.

10914 of 158496 relevant lines covered (6.89%)

6.26 hits per line

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

95.92
/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\Validator\Constraints\All;
26
use Symfony\Component\Validator\Constraints\Choice;
27
use Symfony\Component\Validator\Constraints\Collection;
28
use Symfony\Component\Validator\Constraints\Count;
29
use Symfony\Component\Validator\Constraints\DivisibleBy;
30
use Symfony\Component\Validator\Constraints\GreaterThan;
31
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
32
use Symfony\Component\Validator\Constraints\Length;
33
use Symfony\Component\Validator\Constraints\LessThan;
34
use Symfony\Component\Validator\Constraints\LessThanOrEqual;
35
use Symfony\Component\Validator\Constraints\NotBlank;
36
use Symfony\Component\Validator\Constraints\NotNull;
37
use Symfony\Component\Validator\Constraints\Range;
38
use Symfony\Component\Validator\Constraints\Regex;
39
use Symfony\Component\Validator\Constraints\Type;
40
use Symfony\Component\Validator\Constraints\Unique;
41

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

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

54
        foreach ($resourceMetadataCollection as $i => $resource) {
74✔
55
            $operations = $resource->getOperations();
72✔
56

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

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

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

73
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
72✔
74

75
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
72✔
76
                continue;
34✔
77
            }
78

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

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

90
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
56✔
91
        }
92

93
        return $resourceMetadataCollection;
74✔
94
    }
95

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

102
        $schema ??= $parameter->getSchema();
30✔
103
        $required ??= $parameter->getRequired() ?? false;
30✔
104
        $openApi ??= $parameter->getOpenApi();
30✔
105

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

114
        $assertions = [];
30✔
115

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

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

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

138
        if (isset($schema['pattern'])) {
30✔
139
            $assertions[] = new Regex('#'.$schema['pattern'].'#');
2✔
140
        }
141

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

146
        if (isset($schema['multipleOf'])) {
30✔
147
            $assertions[] = new DivisibleBy(value: $schema['multipleOf']);
2✔
148
        }
149

150
        if (isset($schema['enum'])) {
30✔
151
            $assertions[] = new Choice(choices: $schema['enum']);
10✔
152
        }
153

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

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

163
        if ($parameter->getNativeType()?->isSatisfiedBy(fn ($t) => $t instanceof CollectionType)) {
28✔
164
            $assertions = $assertions ? [new All($assertions)] : [];
16✔
165
        }
166

167
        if ($required && false !== ($allowEmptyValue = $openApi?->getAllowEmptyValue())) {
28✔
168
            $assertions[] = new NotNull(message: \sprintf('The parameter "%s" is required.', $parameter->getKey()));
2✔
169
        }
170

171
        if (isset($schema['minItems']) || isset($schema['maxItems'])) {
28✔
172
            $assertions[] = new Count(min: $schema['minItems'] ?? null, max: $schema['maxItems'] ?? null);
2✔
173
        }
174

175
        if ($schema['uniqueItems'] ?? false) {
28✔
176
            $assertions[] = new Unique();
2✔
177
        }
178

179
        if (isset($schema['type']) && 'array' === $schema['type']) {
28✔
180
            $assertions[] = new Type(type: 'array');
2✔
181
        }
182

183
        if (!$assertions) {
28✔
184
            return $parameter;
24✔
185
        }
186

187
        if (1 === \count($assertions)) {
10✔
188
            return $parameter->withConstraints($assertions[0]);
10✔
189
        }
190

191
        return $parameter->withConstraints($assertions);
2✔
192
    }
193

194
    private function addFilterValidation(HttpOperation $operation): Parameters
195
    {
196
        $parameters = new Parameters();
4✔
197
        $internalPriority = -1;
4✔
198

199
        foreach ($operation->getFilters() as $filter) {
4✔
200
            if (!$this->filterLocator->has($filter)) {
4✔
201
                continue;
×
202
            }
203

204
            $filter = $this->filterLocator->get($filter);
4✔
205
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
4✔
206
                $key = $parameterName;
4✔
207
                $required = $definition['required'] ?? false;
4✔
208
                $schema = $definition['schema'] ?? null;
4✔
209

210
                $openApi = null;
4✔
211
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
4✔
212
                    $openApi = $definition['openapi'];
2✔
213
                }
214

215
                // The query parameter validator forced this, lets maintain BC on filters
216
                if (true === $required && !$openApi) {
4✔
217
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
×
218
                }
219

220
                $parameters->add($key, $this->addSchemaValidation(
4✔
221
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
222
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
4✔
223
                    $schema,
4✔
224
                    $required,
4✔
225
                    $openApi
4✔
226
                ));
4✔
227
            }
228
        }
229

230
        return $parameters;
4✔
231
    }
232
}
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