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

api-platform / core / 12650664077

07 Jan 2025 11:27AM UTC coverage: 62.123% (-0.001%) from 62.124%
12650664077

Pull #6886

github

web-flow
Merge c6a756cfd into f8dae8e11
Pull Request #6886: fix(openapi): not forbidden response on openAPI doc

2 of 2 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

11499 of 18510 relevant lines covered (62.12%)

68.99 hits per line

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

92.11
/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
    }
444✔
42

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

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

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

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

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

82
        if (0 !== \count($constraintViolationList)) {
440✔
83
            throw new ValidationException($constraintViolationList);
48✔
84
        }
85

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

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

94
        if (str_contains($key, '[:property]')) {
48✔
95
            return str_replace('[:property]', $violation->getPropertyPath(), $key);
8✔
96
        }
97

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

102
        if ($p = $violation->getPropertyPath()) {
44✔
UNCOV
103
            return $p;
×
104
        }
105

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