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

api-platform / core / 15181786858

22 May 2025 08:28AM UTC coverage: 27.379% (+0.008%) from 27.371%
15181786858

push

github

soyuka
fix(openapi): nullable externalDocs return type

fixes #7163

13478 of 49228 relevant lines covered (27.38%)

74.67 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
    ) {
49
    }
2,093✔
50

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

55
        foreach ($resourceMetadataCollection as $i => $resource) {
141✔
56
            $operations = $resource->getOperations();
134✔
57

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

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

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

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

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

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

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

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

94
        return $resourceMetadataCollection;
141✔
95
    }
96

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

103
        $schema ??= $parameter->getSchema();
51✔
104
        $required ??= $parameter->getRequired() ?? false;
51✔
105
        $openApi ??= $parameter->getOpenApi();
51✔
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
109
        if (\is_array($openApi)) {
51✔
110
            $openApi = $openApi[0];
11✔
111
        } elseif (false === $openApi) {
49✔
112
            $openApi = null;
22✔
113
        }
114

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

196
        if (!$assertions) {
49✔
197
            return $parameter;
40✔
198
        }
199

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

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

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

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

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

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

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

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

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