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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

0 of 2 new or added lines in 2 files covered. (0.0%)

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
    ) {
UNCOV
41
    }
×
42

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

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

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

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

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

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

UNCOV
90
        if (0 !== \count($constraintViolationList)) {
×
UNCOV
91
            throw new ValidationException($constraintViolationList);
×
92
        }
93

UNCOV
94
        return $this->decorated->provide($operation, $uriVariables, $context);
×
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
    {
UNCOV
100
        $key = $parameter->getKey();
×
101

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

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

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

UNCOV
114
        return $key;
×
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

© 2025 Coveralls, Inc