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

api-platform / core / 14967870200

12 May 2025 08:47AM UTC coverage: 23.685% (+15.2%) from 8.477%
14967870200

push

github

web-flow
fix(serializer): invalid uri variable 400 response (#7135)

1 of 2 new or added lines in 2 files covered. (50.0%)

2825 existing lines in 152 files now uncovered.

11409 of 48169 relevant lines covered (23.69%)

54.89 hits per line

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

83.33
/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
    }
1,284✔
42

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

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

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

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

65
            $violations = [];
449✔
66
            if (\is_array($value) && $properties = $parameter->getExtraProperties()['_properties'] ?? []) {
449✔
UNCOV
67
                foreach ($properties as $property) {
×
UNCOV
68
                    $violations = [...$violations, ...$this->validator->validate($value[$property] ?? null, $constraints)];
×
69
                }
70
            } else {
71
                $violations = $this->validator->validate($value, $constraints);
449✔
72
            }
73

74
            foreach ($violations as $violation) {
449✔
75
                $constraintViolationList->add(new ConstraintViolation(
24✔
76
                    $violation->getMessage(),
24✔
77
                    $violation->getMessageTemplate(),
24✔
78
                    $violation->getParameters(),
24✔
79
                    $violation->getRoot(),
24✔
80
                    $this->getProperty($parameter, $violation),
24✔
81
                    $violation->getInvalidValue(),
24✔
82
                    $violation->getPlural(),
24✔
83
                    $violation->getCode(),
24✔
84
                    $violation->getConstraint(),
24✔
85
                    $violation->getCause()
24✔
86
                ));
24✔
87
            }
88
        }
89

90
        if (0 !== \count($constraintViolationList)) {
1,284✔
91
            throw new ValidationException($constraintViolationList);
24✔
92
        }
93

94
        return $this->decorated->provide($operation, $uriVariables, $context);
1,284✔
95
    }
96

97
    // There's a `property` inside Parameter but it's used for hydra:search only as here we want the parameter name instead
98
    private function getProperty(Parameter $parameter, ConstraintViolationInterface $violation): string
99
    {
100
        $key = $parameter->getKey();
24✔
101

102
        if (str_contains($key, '[:property]')) {
24✔
UNCOV
103
            return str_replace('[:property]', $violation->getPropertyPath(), $key);
×
104
        }
105

106
        if (str_contains($key, ':property')) {
24✔
107
            return str_replace(':property', $violation->getPropertyPath(), $key);
×
108
        }
109

110
        if ($p = $violation->getPropertyPath()) {
24✔
111
            return $p;
×
112
        }
113

114
        return $key;
24✔
115
    }
116
}
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