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

api-platform / core / 15256141101

26 May 2025 02:16PM UTC coverage: 21.714% (-4.8%) from 26.526%
15256141101

push

github

web-flow
Merge 4.1

6 of 387 new or added lines in 28 files covered. (1.55%)

1891 existing lines in 117 files now uncovered.

11118 of 51201 relevant lines covered (21.71%)

29.33 hits per line

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

86.84
/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\Operation;
17
use ApiPlatform\Metadata\Parameter;
18
use ApiPlatform\State\ParameterNotFound;
19
use ApiPlatform\State\ProviderInterface;
20
use ApiPlatform\State\Util\ParameterParserTrait;
21
use ApiPlatform\Validator\Exception\ValidationException;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\Validator\ConstraintViolation;
24
use Symfony\Component\Validator\ConstraintViolationInterface;
25
use Symfony\Component\Validator\ConstraintViolationList;
26
use Symfony\Component\Validator\Validator\ValidatorInterface;
27

28
/**
29
 * Validates parameters using the Symfony validator.
30
 *
31
 * @experimental
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
    }
710✔
42

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

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

54
        $constraintViolationList = new ConstraintViolationList();
710✔
55
        foreach ($operation->getParameters() ?? [] as $parameter) {
710✔
56
            if (!$constraints = $parameter->getConstraints()) {
251✔
57
                continue;
249✔
58
            }
59

60
            $value = $parameter->getValue();
200✔
61
            if ($value instanceof ParameterNotFound) {
200✔
62
                $value = null;
39✔
63
            }
64

65
            $violations = $this->validator->validate($value, $constraints);
200✔
66

67
            foreach ($violations as $violation) {
200✔
68
                $constraintViolationList->add(new ConstraintViolation(
12✔
69
                    $violation->getMessage(),
12✔
70
                    $violation->getMessageTemplate(),
12✔
71
                    $violation->getParameters(),
12✔
72
                    $violation->getRoot(),
12✔
73
                    $this->getProperty($parameter, $violation),
12✔
74
                    $violation->getInvalidValue(),
12✔
75
                    $violation->getPlural(),
12✔
76
                    $violation->getCode(),
12✔
77
                    $violation->getConstraint(),
12✔
78
                    $violation->getCause()
12✔
79
                ));
12✔
80
            }
81
        }
82

83
        if (0 !== \count($constraintViolationList)) {
710✔
84
            throw new ValidationException($constraintViolationList);
12✔
85
        }
86

87
        return $this->decorated->provide($operation, $uriVariables, $context);
710✔
88
    }
89

90
    // There's a `property` inside Parameter but it's used for hydra:search only as here we want the parameter name instead
91
    private function getProperty(Parameter $parameter, ConstraintViolationInterface $violation): string
92
    {
93
        $key = $parameter->getKey();
12✔
94

95
        if (str_contains($key, '[:property]')) {
12✔
UNCOV
96
            return str_replace('[:property]', $violation->getPropertyPath(), $key);
×
97
        }
98

99
        if (str_contains($key, ':property')) {
12✔
100
            return str_replace(':property', $violation->getPropertyPath(), $key);
×
101
        }
102

103
        if ($p = $violation->getPropertyPath()) {
12✔
UNCOV
104
            return $key.$p;
×
105
        }
106

107
        return $key;
12✔
108
    }
109
}
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