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

api-platform / core / 15966359730

30 Jun 2025 07:13AM UTC coverage: 22.064% (-0.02%) from 22.082%
15966359730

push

github

soyuka
ci: distribution update on new tag only

11518 of 52203 relevant lines covered (22.06%)

22.04 hits per line

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

96.23
/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 ApiPlatform\Validator\Util\ParameterValidationConstraints;
24
use Psr\Container\ContainerInterface;
25

26
/**
27
 * @experimental
28
 */
29
final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
30
{
31
    use ParameterValidationConstraints;
32

33
    public function __construct(
34
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
35
        private readonly ?ContainerInterface $filterLocator = null,
36
    ) {
37
    }
588✔
38

39
    public function create(string $resourceClass): ResourceMetadataCollection
40
    {
41
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
102✔
42

43
        foreach ($resourceMetadataCollection as $i => $resource) {
102✔
44
            $operations = $resource->getOperations();
100✔
45

46
            foreach ($operations as $operationName => $operation) {
100✔
47
                $parameters = $operation->getParameters() ?? new Parameters();
100✔
48
                foreach ($parameters as $key => $parameter) {
100✔
49
                    $parameters->add($key, $this->addSchemaValidation($parameter));
28✔
50
                }
51

52
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
53
                if ($operation->getFilters() && 0 === $parameters->count()) {
100✔
54
                    $parameters = $this->addFilterValidation($operation);
6✔
55
                }
56

57
                if (\count($parameters) > 0) {
100✔
58
                    $operations->add($operationName, $operation->withParameters($parameters));
34✔
59
                }
60
            }
61

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

64
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
100✔
65
                continue;
52✔
66
            }
67

68
            foreach ($graphQlOperations as $operationName => $operation) {
72✔
69
                $parameters = $operation->getParameters() ?? new Parameters();
72✔
70
                foreach ($operation->getParameters() ?? [] as $key => $parameter) {
72✔
71
                    $parameters->add($key, $this->addSchemaValidation($parameter));
2✔
72
                }
73

74
                if (\count($parameters) > 0) {
72✔
75
                    $graphQlOperations[$operationName] = $operation->withParameters($parameters);
2✔
76
                }
77
            }
78

79
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
72✔
80
        }
81

82
        return $resourceMetadataCollection;
102✔
83
    }
84

85
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): Parameter
86
    {
87
        if (null !== $parameter->getConstraints()) {
34✔
88
            return $parameter;
4✔
89
        }
90

91
        $assertions = $this->getParameterValidationConstraints($parameter, $schema, $required, $openApi);
32✔
92
        if (!$assertions) {
32✔
93
            return $parameter;
28✔
94
        }
95

96
        if (1 === \count($assertions)) {
12✔
97
            return $parameter->withConstraints($assertions[0]);
12✔
98
        }
99

100
        return $parameter->withConstraints($assertions);
2✔
101
    }
102

103
    private function addFilterValidation(HttpOperation $operation): Parameters
104
    {
105
        $parameters = new Parameters();
6✔
106
        $internalPriority = -1;
6✔
107

108
        foreach ($operation->getFilters() as $filter) {
6✔
109
            if (!$this->filterLocator->has($filter)) {
6✔
110
                continue;
×
111
            }
112

113
            $filter = $this->filterLocator->get($filter);
6✔
114
            foreach ($filter->getDescription($operation->getClass()) as $parameterName => $definition) {
6✔
115
                $key = $parameterName;
6✔
116
                $required = $definition['required'] ?? false;
6✔
117
                $schema = $definition['schema'] ?? null;
6✔
118

119
                $openApi = null;
6✔
120
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
6✔
121
                    $openApi = $definition['openapi'];
2✔
122
                }
123

124
                // The query parameter validator forced this, lets maintain BC on filters
125
                if (true === $required && !$openApi) {
6✔
126
                    $openApi = new OpenApiParameter(name: $key, in: 'query', allowEmptyValue: false);
×
127
                }
128

129
                $parameters->add($key, $this->addSchemaValidation(
6✔
130
                    // we disable openapi and hydra on purpose as their docs comes from filters see the condition for addFilterValidation above
131
                    new QueryParameter(key: $key, property: $definition['property'] ?? null, priority: $internalPriority--, schema: $schema, openApi: false, hydra: false),
6✔
132
                    $schema,
6✔
133
                    $required,
6✔
134
                    $openApi
6✔
135
                ));
6✔
136
            }
137
        }
138

139
        return $parameters;
6✔
140
    }
141
}
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