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

api-platform / core / 18018574401

25 Sep 2025 07:30PM UTC coverage: 21.847% (-0.04%) from 21.883%
18018574401

push

github

soyuka
Merge 4.1

0 of 8 new or added lines in 1 file covered. (0.0%)

3 existing lines in 1 file now uncovered.

11880 of 54379 relevant lines covered (21.85%)

25.41 hits per line

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

88.24
/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
final class ParameterValidatorProvider implements ProviderInterface
34
{
35
    use ParameterParserTrait;
36

37
    public function __construct(
38
        private readonly ValidatorInterface $validator,
39
        private readonly ProviderInterface $decorated,
40
    ) {
41
    }
631✔
42

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

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

54
        $constraintViolationList = new ConstraintViolationList();
601✔
55
        $parameters = $operation->getParameters() ?? new Parameters();
601✔
56

57
        if ($operation instanceof HttpOperation) {
601✔
58
            foreach ($operation->getUriVariables() ?? [] as $key => $uriVariable) {
601✔
59
                if ($uriVariable->getValue() instanceof ParameterNotFound) {
222✔
60
                    $uriVariable->setValue($uriVariables[$key] ?? new ParameterNotFound());
218✔
61
                }
62

63
                $parameters->add($key, $uriVariable->withKey($key));
222✔
64
            }
65
        }
66

67
        foreach ($parameters as $parameter) {
601✔
68
            if (!$constraints = $parameter->getConstraints()) {
532✔
69
                continue;
502✔
70
            }
71

72
            $value = $parameter->getValue();
188✔
73

74
            if ($value instanceof ParameterNotFound) {
188✔
75
                $value = null;
78✔
76
            }
77

78
            $violations = $this->validator->validate($value, $constraints);
188✔
79

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

96
        if (0 !== \count($constraintViolationList)) {
601✔
97
            throw new ValidationException($constraintViolationList);
64✔
98
        }
99

100
        return $this->decorated->provide($operation, $uriVariables, $context);
601✔
101
    }
102

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

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

112
        if (str_contains($key, ':property')) {
60✔
113
            return str_replace(':property', $violation->getPropertyPath(), $key);
×
114
        }
115

116
        $openApi = $parameter->getOpenApi();
60✔
117
        if (false === $openApi) {
60✔
118
            $openApi = null;
×
119
        }
120

121
        if (\is_array($openApi)) {
60✔
UNCOV
122
            foreach ($openApi as $oa) {
×
UNCOV
123
                if ('deepObject' === $oa->getStyle() && ($oa->getName() === $key || str_starts_with($oa->getName(), $key.'['))) {
×
UNCOV
124
                    return $key.$violation->getPropertyPath();
×
125
                }
126
            }
127
        } elseif ('deepObject' === $openApi?->getStyle() && $p = $violation->getPropertyPath()) {
60✔
128
            return $key.$p;
2✔
129
        }
130

131
        return $key;
60✔
132
    }
133
}
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