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

api-platform / core / 20730978299

05 Jan 2026 10:22PM UTC coverage: 24.505% (-4.4%) from 28.866%
20730978299

Pull #7647

github

web-flow
Merge 706a84846 into 7d4b5e9c5
Pull Request #7647: fix(doctrine): fix partial fetch with same entity included multiple time with different fields

23 of 60 new or added lines in 2 files covered. (38.33%)

837 existing lines in 70 files now uncovered.

14251 of 58156 relevant lines covered (24.5%)

14.82 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
final class ParameterValidationResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
27
{
28
    use ParameterValidationConstraints;
29

30
    public function __construct(
31
        private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
32
        private readonly ?ContainerInterface $filterLocator = null,
33
    ) {
34
    }
427✔
35

36
    public function create(string $resourceClass): ResourceMetadataCollection
37
    {
38
        $resourceMetadataCollection = $this->decorated?->create($resourceClass) ?? new ResourceMetadataCollection($resourceClass);
68✔
39

40
        foreach ($resourceMetadataCollection as $i => $resource) {
68✔
41
            $operations = $resource->getOperations();
67✔
42

43
            foreach ($operations as $operationName => $operation) {
67✔
44
                $parameters = $operation->getParameters() ?? new Parameters();
67✔
45
                foreach ($parameters as $key => $parameter) {
67✔
46
                    $parameters->add($key, $this->addSchemaValidation($parameter));
18✔
47
                }
48

49
                // As we deprecate the parameter validator, we declare a parameter for each filter transfering validation to the new system
50
                if ($operation->getFilters() && 0 === $parameters->count()) {
67✔
51
                    $parameters = $this->addFilterValidation($operation);
3✔
52
                }
53

54
                if (\count($parameters) > 0) {
67✔
55
                    $operations->add($operationName, $operation->withParameters($parameters));
21✔
56
                }
57
            }
58

59
            $resourceMetadataCollection[$i] = $resource->withOperations($operations->sort());
67✔
60

61
            if (!$graphQlOperations = $resource->getGraphQlOperations()) {
67✔
62
                continue;
32✔
63
            }
64

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

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

76
            $resourceMetadataCollection[$i] = $resource->withGraphQlOperations($graphQlOperations);
48✔
77
        }
78

79
        return $resourceMetadataCollection;
68✔
80
    }
81

82
    private function addSchemaValidation(Parameter $parameter, ?array $schema = null, ?bool $required = null, ?OpenApiParameter $openApi = null): Parameter
83
    {
84
        if (null !== $parameter->getConstraints()) {
21✔
85
            return $parameter;
3✔
86
        }
87

88
        $assertions = $this->getParameterValidationConstraints($parameter, $schema, $required, $openApi);
20✔
89
        if (!$assertions) {
20✔
90
            return $parameter;
16✔
91
        }
92

93
        if (1 === \count($assertions)) {
7✔
94
            return $parameter->withConstraints($assertions[0]);
7✔
95
        }
96

97
        return $parameter->withConstraints($assertions);
1✔
98
    }
99

100
    private function addFilterValidation(HttpOperation $operation): Parameters
101
    {
102
        $parameters = new Parameters();
3✔
103
        $internalPriority = -1;
3✔
104

105
        foreach ($operation->getFilters() as $filter) {
3✔
106
            if (!$this->filterLocator->has($filter)) {
3✔
UNCOV
107
                continue;
×
108
            }
109

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

116
                $openApi = null;
3✔
117
                if (isset($definition['openapi']) && $definition['openapi'] instanceof OpenApiParameter) {
3✔
118
                    $openApi = $definition['openapi'];
1✔
119
                }
120

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

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

136
        return $parameters;
3✔
137
    }
138
}
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

© 2026 Coveralls, Inc