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

api-platform / core / 20001323174

07 Dec 2025 08:10AM UTC coverage: 25.292% (+0.001%) from 25.291%
20001323174

push

github

soyuka
chore: bump metadata to 4.3.x-dev

14642 of 57891 relevant lines covered (25.29%)

28.94 hits per line

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

89.36
/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
    }
731✔
42

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

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

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

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

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

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

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

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

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

80
            foreach ($violations as $violation) {
190✔
81
                $constraintViolationList->add(new ConstraintViolation(
66✔
82
                    $violation->getMessage(),
66✔
83
                    $violation->getMessageTemplate(),
66✔
84
                    $violation->getParameters(),
66✔
85
                    $violation->getRoot(),
66✔
86
                    $this->getProperty($parameter, $violation),
66✔
87
                    $violation->getInvalidValue(),
66✔
88
                    $violation->getPlural(),
66✔
89
                    $violation->getCode(),
66✔
90
                    // TODO: remove these with symfony ^7
91
                    method_exists($violation, 'getConstraint') ? $violation->getConstraint() : null, // @phpstan-ignore-line symfony/validator 6.4 is still allowed and this may be true
66✔
92
                    method_exists($violation, 'getCause') ? $violation->getCause() : null // @phpstan-ignore-line symfony/validator 6.4 is still allowed and this may be true
66✔
93
                ));
66✔
94
            }
95
        }
96

97
        if (0 !== \count($constraintViolationList)) {
683✔
98
            throw new ValidationException($constraintViolationList);
66✔
99
        }
100

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

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

109
        if (false === $openApi) {
66✔
110
            $openApi = null;
×
111
        }
112

113
        $key = $parameter->getKey();
66✔
114
        if (\is_array($openApi)) {
66✔
115
            foreach ($openApi as $oa) {
×
116
                if ('deepObject' === $oa->getStyle() && ($oa->getName() === $key || str_starts_with($oa->getName(), $key.'['))) {
×
117
                    return $key.$violation->getPropertyPath();
×
118
                }
119
            }
120
        } elseif ('deepObject' === $openApi?->getStyle() && $p = $violation->getPropertyPath()) {
66✔
121
            return $key.$p;
4✔
122
        }
123

124
        return $key;
64✔
125
    }
126
}
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