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

api-platform / core / 16531587208

25 Jul 2025 09:05PM UTC coverage: 0.0% (-22.1%) from 22.07%
16531587208

Pull #7225

github

web-flow
Merge 23f449a58 into 02a764950
Pull Request #7225: feat: json streamer

0 of 294 new or added lines in 31 files covered. (0.0%)

11514 existing lines in 375 files now uncovered.

0 of 51976 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
    ) {
UNCOV
37
    }
×
38

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

UNCOV
43
        foreach ($resourceMetadataCollection as $i => $resource) {
×
UNCOV
44
            $operations = $resource->getOperations();
×
45

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

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

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

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

UNCOV
64
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
×
UNCOV
65
                continue;
×
66
            }
67

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

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

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

UNCOV
82
        return $resourceMetadataCollection;
×
83
    }
84

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

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

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

UNCOV
100
        return $parameter->withConstraints($assertions);
×
101
    }
102

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

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

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

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

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

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

UNCOV
139
        return $parameters;
×
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