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

api-platform / core / 10323454690

09 Aug 2024 05:46PM UTC coverage: 7.841%. Remained the same
10323454690

push

github

soyuka
Merge 3.4

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

1896 existing lines in 118 files now uncovered.

12688 of 161818 relevant lines covered (7.84%)

26.86 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
    {
49
    }
2,671✔
50

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

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

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

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

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

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

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

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

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

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

100
        return $resourceMetadataCollection;
111✔
101
    }
102

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

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

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

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

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

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

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

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

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

143
        return $parameter;
15✔
144
    }
145

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

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

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

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

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

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

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

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

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

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

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

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

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

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

202
        if (!$assertions) {
28✔
203
            return $parameter;
25✔
204
        }
205

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

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

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

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

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

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

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

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

249
        return $parameters;
21✔
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