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

api-platform / core / 10903050455

17 Sep 2024 12:29PM UTC coverage: 7.684% (+0.7%) from 6.96%
10903050455

push

github

web-flow
fix: swagger ui with route identifier (#6616)

2 of 6 new or added lines in 6 files covered. (33.33%)

9000 existing lines in 286 files now uncovered.

12668 of 164863 relevant lines covered (7.68%)

22.93 hits per line

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

0.0
/src/Laravel/Controller/ApiPlatformController.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\Laravel\Controller;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactory;
18
use ApiPlatform\State\ProcessorInterface;
19
use ApiPlatform\State\ProviderInterface;
20
use Illuminate\Foundation\Application;
21
use Illuminate\Http\Request;
22
use Illuminate\Routing\Controller;
23
use Symfony\Component\HttpFoundation\Response;
24

25
class ApiPlatformController extends Controller
26
{
27
    /**
28
     * @param ProviderInterface<object>                                  $provider
29
     * @param ProcessorInterface<iterable<object>|object|null, Response> $processor
30
     */
31
    public function __construct(
32
        protected OperationMetadataFactory $operationMetadataFactory,
33
        protected ProviderInterface $provider,
34
        protected ProcessorInterface $processor,
35
        protected Application $app,
36
    ) {
37
    }
×
38

39
    /**
40
     * Display a listing of the resource.
41
     */
42
    public function __invoke(Request $request): Response
43
    {
44
        $operation = $request->attributes->get('_api_operation');
×
45
        if (!$operation) {
×
46
            throw new \RuntimeException('Operation not found.');
×
47
        }
48

49
        if (!$operation instanceof HttpOperation) {
×
50
            throw new \LogicException('Operation is not an HttpOperation.');
×
51
        }
52

53
        $uriVariables = $this->getUriVariables($request, $operation);
×
NEW
54
        $request->attributes->set('_api_uri_variables', $uriVariables);
×
55
        // at some point we could introduce that back
56
        // if ($this->uriVariablesConverter) {
57
        //     $context = ['operation' => $operation, 'uri_variables_map' => $uriVariablesMap];
58
        //     $identifiers = $this->uriVariablesConverter->convert($identifiers, $operation->getClass() ?? $resourceClass, $context);
59
        // }
60

61
        $context = [
×
62
            'request' => $request,
×
63
            'uri_variables' => $uriVariables,
×
64
            'resource_class' => $operation->getClass(),
×
65
        ];
×
66

67
        if (null === $operation->canValidate()) {
×
68
            $operation = $operation->withValidate(!$request->isMethodSafe() && !$request->isMethod('DELETE'));
×
69
        }
70

71
        if (null === $operation->canRead()) {
×
72
            $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe()); // @phpstan-ignore-line
×
73
        }
74

75
        if (null === $operation->canDeserialize()) {
×
76
            $operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true)); // @phpstan-ignore-line
×
77
        }
78

79
        $body = $this->provider->provide($operation, $uriVariables, $context);
×
80

81
        // The provider can change the Operation, extract it again from the Request attributes
82
        if ($request->attributes->get('_api_operation') !== $operation) {
×
83
            $operation = $request->attributes->get('_api_operation');
×
84
            $uriVariables = $this->getUriVariables($request, $operation);
×
85
        }
86

87
        $context['previous_data'] = $request->attributes->get('previous_data');
×
88
        $context['data'] = $request->attributes->get('data');
×
89

90
        if (null === $operation->canWrite()) {
×
91
            $operation = $operation->withWrite(!$request->isMethodSafe());
×
92
        }
93

94
        if (null === $operation->canSerialize()) {
×
95
            $operation = $operation->withSerialize(true);
×
96
        }
97

98
        return $this->processor->process($body, $operation, $uriVariables, $context);
×
99
    }
100

101
    /**
102
     * @return array<string, mixed>
103
     */
104
    private function getUriVariables(Request $request, HttpOperation $operation): array
105
    {
106
        $uriVariables = [];
×
107
        foreach ($operation->getUriVariables() ?? [] as $parameterName => $_) {
×
108
            $parameter = $request->route($parameterName);
×
109
            if (\is_string($parameter) && ($format = $request->attributes->get('_format')) && str_contains($parameter, $format)) {
×
110
                $parameter = substr($parameter, 0, \strlen($parameter) - (\strlen($format) + 1));
×
111
            }
112

113
            $uriVariables[(string) $parameterName] = $parameter;
×
114
        }
115

116
        return $uriVariables;
×
117
    }
118
}
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