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

api-platform / core / 15775135891

20 Jun 2025 08:42AM UTC coverage: 22.065% (+0.2%) from 21.876%
15775135891

push

github

soyuka
Merge 4.1

13 of 103 new or added lines in 10 files covered. (12.62%)

868 existing lines in 35 files now uncovered.

11487 of 52060 relevant lines covered (22.06%)

21.72 hits per line

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

95.45
/src/Symfony/Validator/State/ParameterValidatorProvider.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\Symfony\Validator\State;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\Metadata\Parameter;
19
use ApiPlatform\Metadata\Parameters;
20
use ApiPlatform\State\ParameterNotFound;
21
use ApiPlatform\State\ProviderInterface;
22
use ApiPlatform\State\Util\ParameterParserTrait;
23
use ApiPlatform\Validator\Exception\ValidationException;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\Validator\ConstraintViolation;
26
use Symfony\Component\Validator\ConstraintViolationInterface;
27
use Symfony\Component\Validator\ConstraintViolationList;
28
use Symfony\Component\Validator\Validator\ValidatorInterface;
29

30
/**
31
 * Validates parameters using the Symfony validator.
32
 *
33
 * @experimental
34
 */
35
final class ParameterValidatorProvider implements ProviderInterface
36
{
37
    use ParameterParserTrait;
38

39
    public function __construct(
40
        private readonly ValidatorInterface $validator,
41
        private readonly ProviderInterface $decorated,
42
    ) {
43
    }
489✔
44

45
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
46
    {
47
        if (!($request = $context['request'] ?? null) instanceof Request) {
461✔
UNCOV
48
            return $this->decorated->provide($operation, $uriVariables, $context);
×
49
        }
50

51
        $operation = $request->attributes->get('_api_operation') ?? $operation;
461✔
52
        if (!($operation->getQueryParameterValidationEnabled() ?? true)) {
461✔
53
            return $this->decorated->provide($operation, $uriVariables, $context);
2✔
54
        }
55

56
        $constraintViolationList = new ConstraintViolationList();
459✔
57
        $parameters = $operation->getParameters() ?? new Parameters();
459✔
58

59
        if ($operation instanceof HttpOperation) {
459✔
60
            foreach ($operation->getUriVariables() ?? [] as $key => $uriVariable) {
459✔
61
                if ($uriVariable->getValue() instanceof ParameterNotFound) {
172✔
62
                    $uriVariable->setValue($uriVariables[$key] ?? new ParameterNotFound());
172✔
63
                }
64

65
                $parameters->add($key, $uriVariable->withKey($key));
172✔
66
            }
67
        }
68

69
        foreach ($parameters as $parameter) {
459✔
70
            if (!$constraints = $parameter->getConstraints()) {
396✔
71
                continue;
372✔
72
            }
73

74
            $value = $parameter->getValue();
112✔
75

76
            if ($value instanceof ParameterNotFound) {
112✔
77
                $value = null;
68✔
78
            }
79

80
            $violations = $this->validator->validate($value, $constraints);
112✔
81

82
            foreach ($violations as $violation) {
112✔
83
                $constraintViolationList->add(new ConstraintViolation(
38✔
84
                    $violation->getMessage(),
38✔
85
                    $violation->getMessageTemplate(),
38✔
86
                    $violation->getParameters(),
38✔
87
                    $violation->getRoot(),
38✔
88
                    $this->getProperty($parameter, $violation),
38✔
89
                    $violation->getInvalidValue(),
38✔
90
                    $violation->getPlural(),
38✔
91
                    $violation->getCode(),
38✔
92
                    $violation->getConstraint(),
38✔
93
                    $violation->getCause()
38✔
94
                ));
38✔
95
            }
96
        }
97

98
        if (0 !== \count($constraintViolationList)) {
459✔
99
            throw new ValidationException($constraintViolationList);
38✔
100
        }
101

102
        return $this->decorated->provide($operation, $uriVariables, $context);
459✔
103
    }
104

105
    // There's a `property` inside Parameter but it's used for hydra:search only as here we want the parameter name instead
106
    private function getProperty(Parameter $parameter, ConstraintViolationInterface $violation): string
107
    {
108
        $key = $parameter->getKey();
38✔
109

110
        if (str_contains($key, '[:property]')) {
38✔
111
            return str_replace('[:property]', $violation->getPropertyPath(), $key);
6✔
112
        }
113

114
        if (str_contains($key, ':property')) {
34✔
UNCOV
115
            return str_replace(':property', $violation->getPropertyPath(), $key);
×
116
        }
117

118
        if ($p = $violation->getPropertyPath()) {
34✔
119
            return $key.$p;
8✔
120
        }
121

122
        return $key;
28✔
123
    }
124
}
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