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

api-platform / core / 9810759639

05 Jul 2024 03:48PM UTC coverage: 63.445% (+0.07%) from 63.374%
9810759639

push

github

web-flow
fix(state): query and header parameter with the same name (#6453)

65 of 73 new or added lines in 10 files covered. (89.04%)

1 existing line in 1 file now uncovered.

11172 of 17609 relevant lines covered (63.44%)

52.83 hits per line

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

94.29
/src/State/Provider/ParameterProvider.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\State\Provider;
15

16
use ApiPlatform\Metadata\Operation;
17
use ApiPlatform\State\Exception\ProviderNotFoundException;
18
use ApiPlatform\State\ParameterNotFound;
19
use ApiPlatform\State\ParameterProviderInterface;
20
use ApiPlatform\State\ProviderInterface;
21
use ApiPlatform\State\Util\ParameterParserTrait;
22
use ApiPlatform\State\Util\RequestParser;
23
use Psr\Container\ContainerInterface;
24

25
/**
26
 * Loops over parameters to:
27
 *   - compute its values set as extra properties from the Parameter object (`_api_values`)
28
 *   - call the Parameter::provider if any and updates the Operation
29
 *
30
 * @experimental
31
 */
32
final class ParameterProvider implements ProviderInterface
33
{
34
    use ParameterParserTrait;
35

36
    public function __construct(private readonly ?ProviderInterface $decorated = null, private readonly ?ContainerInterface $locator = null)
37
    {
38
    }
255✔
39

40
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
41
    {
42
        $request = $context['request'] ?? null;
255✔
43
        if ($request && null === $request->attributes->get('_api_query_parameters')) {
255✔
44
            $queryString = RequestParser::getQueryString($request);
252✔
45
            $request->attributes->set('_api_query_parameters', $queryString ? RequestParser::parseRequestParams($queryString) : []);
252✔
46
        }
47

48
        if ($request && null === $request->attributes->get('_api_header_parameters')) {
255✔
49
            $request->attributes->set('_api_header_parameters', $request->headers->all());
252✔
50
        }
51

52
        $context = ['operation' => $operation] + $context;
255✔
53
        $parameters = $operation->getParameters();
255✔
54
        foreach ($parameters ?? [] as $parameter) {
255✔
55
            $values = $this->getParameterValues($parameter, $request, $context);
87✔
56
            $value = $this->extractParameterValues($parameter, $values);
87✔
57

58
            if ((!$value || $value instanceof ParameterNotFound) && ($default = $parameter->getSchema()['default'] ?? false)) {
87✔
NEW
59
                $value = $default;
×
60
            }
61

62
            if ($value instanceof ParameterNotFound) {
87✔
63
                continue;
83✔
64
            }
65

66
            $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties(
79✔
67
                $parameter->getExtraProperties() + ['_api_values' => [$parameter->getKey() => $value]]
79✔
68
            ));
79✔
69

70
            if (null === ($provider = $parameter->getProvider())) {
79✔
71
                continue;
59✔
72
            }
73

74
            if (\is_callable($provider)) {
20✔
75
                if (($op = $provider($parameter, $values, $context)) instanceof Operation) {
8✔
76
                    $operation = $op;
4✔
77
                }
78

79
                continue;
4✔
80
            }
81

82
            if (!\is_string($provider) || !$this->locator->has($provider)) {
16✔
83
                throw new ProviderNotFoundException(sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
×
84
            }
85

86
            /** @var ParameterProviderInterface $providerInstance */
87
            $providerInstance = $this->locator->get($provider);
16✔
88
            if (($op = $providerInstance->provide($parameter, $values, $context)) instanceof Operation) {
16✔
89
                $operation = $op;
12✔
90
            }
91
        }
92

93
        if ($parameters) {
255✔
94
            $operation = $operation->withParameters($parameters);
191✔
95
        }
96
        $request?->attributes->set('_api_operation', $operation);
255✔
97
        $context['operation'] = $operation;
255✔
98

99
        return $this->decorated?->provide($operation, $uriVariables, $context);
255✔
100
    }
101
}
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