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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

97.83
/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\HttpOperation;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\State\Exception\ParameterNotSupportedException;
19
use ApiPlatform\State\Exception\ProviderNotFoundException;
20
use ApiPlatform\State\ParameterNotFound;
21
use ApiPlatform\State\ProviderInterface;
22
use ApiPlatform\State\Util\ParameterParserTrait;
23
use ApiPlatform\State\Util\RequestParser;
24
use Psr\Container\ContainerInterface;
25

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

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

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

UNCOV
49
        if ($request && null === $request->attributes->get('_api_header_parameters')) {
922✔
UNCOV
50
            $request->attributes->set('_api_header_parameters', $request->headers->all());
799✔
51
        }
52

UNCOV
53
        $parameters = $operation->getParameters();
922✔
54

UNCOV
55
        if ($operation instanceof HttpOperation && true === $operation->getStrictQueryParameterValidation()) {
922✔
UNCOV
56
            $keys = [];
2✔
UNCOV
57
            foreach ($parameters as $parameter) {
2✔
UNCOV
58
                $keys[] = $parameter->getKey();
2✔
59
            }
60

UNCOV
61
            foreach (array_keys($request->attributes->get('_api_query_parameters')) as $key) {
2✔
UNCOV
62
                if (!\in_array($key, $keys, true)) {
2✔
UNCOV
63
                    throw new ParameterNotSupportedException($key);
1✔
64
                }
65
            }
66
        }
67

UNCOV
68
        foreach ($parameters ?? [] as $parameter) {
922✔
UNCOV
69
            $extraProperties = $parameter->getExtraProperties();
375✔
UNCOV
70
            unset($extraProperties['_api_values']);
375✔
UNCOV
71
            $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties($extraProperties));
375✔
72

UNCOV
73
            $context = ['operation' => $operation] + $context;
375✔
UNCOV
74
            $values = $this->getParameterValues($parameter, $request, $context);
375✔
UNCOV
75
            $value = $this->extractParameterValues($parameter, $values);
375✔
76

UNCOV
77
            if (($default = $parameter->getSchema()['default'] ?? false) && ($value instanceof ParameterNotFound || !$value)) {
375✔
UNCOV
78
                $value = $default;
159✔
79
            }
80

UNCOV
81
            if ($value instanceof ParameterNotFound) {
375✔
UNCOV
82
                continue;
366✔
83
            }
84

UNCOV
85
            $parameters->add($parameter->getKey(), $parameter = $parameter->withExtraProperties(
344✔
UNCOV
86
                $parameter->getExtraProperties() + ['_api_values' => $value]
344✔
UNCOV
87
            ));
344✔
88

UNCOV
89
            if (null === ($provider = $parameter->getProvider())) {
344✔
UNCOV
90
                continue;
336✔
91
            }
92

UNCOV
93
            if (\is_callable($provider)) {
8✔
UNCOV
94
                if (($op = $provider($parameter, $values, $context)) instanceof Operation) {
4✔
UNCOV
95
                    $operation = $op;
3✔
96
                }
97

UNCOV
98
                continue;
3✔
99
            }
100

UNCOV
101
            if (\is_string($provider)) {
5✔
UNCOV
102
                if (!$this->locator->has($provider)) {
5✔
103
                    throw new ProviderNotFoundException(\sprintf('Provider "%s" not found on operation "%s"', $provider, $operation->getName()));
×
104
                }
105

UNCOV
106
                $provider = $this->locator->get($provider);
5✔
107
            }
108

UNCOV
109
            if (($op = $provider->provide($parameter, $values, $context)) instanceof Operation) {
5✔
UNCOV
110
                $operation = $op;
5✔
111
            }
112
        }
113

UNCOV
114
        if ($parameters) {
922✔
UNCOV
115
            $operation = $operation->withParameters($parameters);
374✔
116
        }
UNCOV
117
        $request?->attributes->set('_api_operation', $operation);
922✔
UNCOV
118
        $context['operation'] = $operation;
922✔
119

UNCOV
120
        return $this->decorated?->provide($operation, $uriVariables, $context);
922✔
121
    }
122
}
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