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

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 hits per line

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

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

16
use ApiPlatform\Documentation\Documentation;
17
use ApiPlatform\Metadata\Error;
18
use ApiPlatform\Metadata\Get;
19
use ApiPlatform\Metadata\HttpOperation;
20
use ApiPlatform\Metadata\Operation;
21
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
22
use ApiPlatform\OpenApi\OpenApi;
23
use ApiPlatform\State\ProviderInterface;
24

25
/**
26
 * When an HTML request is sent we provide a swagger ui documentation.
27
 *
28
 * @implements ProviderInterface<OpenApi|object>
29
 *
30
 * @internal
31
 */
32
final class SwaggerUiProvider implements ProviderInterface
33
{
34
    /**
35
     * @param ProviderInterface<object> $decorated
36
     */
37
    public function __construct(
38
        private readonly ProviderInterface $decorated,
39
        private readonly OpenApiFactoryInterface $openApiFactory,
40
        private readonly bool $swaggerUiEnabled = true,
41
    ) {
42
    }
×
43

44
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
45
    {
46
        // We went through the DocumentationAction
47
        if (OpenApi::class === $operation->getClass()) {
×
48
            return $this->decorated->provide($operation, $uriVariables, $context);
×
49
        }
50

51
        if (
52
            !($operation instanceof HttpOperation)
×
53
            || !($request = $context['request'] ?? null)
×
54
            || 'html' !== $request->getRequestFormat()
×
55
            || !$this->swaggerUiEnabled
×
NEW
56
            || true === ($operation->getExtraProperties()['_api_disable_swagger_provider'] ?? false)
×
57
        ) {
58
            return $this->decorated->provide($operation, $uriVariables, $context);
×
59
        }
60

61
        if (!$request->attributes->has('_api_requested_operation')) {
×
62
            $request->attributes->set('_api_requested_operation', $operation);
×
63
        }
64

65
        // We need to call our operation provider just in case it fails
66
        // when it fails we'll get an Error, and we'll fix the status accordingly
67
        // @see features/main/content_negotiation.feature:119
68
        // When requesting DocumentationAction or EntrypointAction with Accept: text/html we render SwaggerUi
69
        if (!$operation instanceof Error && Documentation::class !== $operation->getClass()) {
×
70
            $this->decorated->provide($operation, $uriVariables, $context);
×
71
        }
72

73
        $swaggerUiOperation = new Get(
×
74
            class: OpenApi::class,
×
75
            processor: 'api_platform.swagger_ui.processor',
×
76
            validate: false,
×
77
            read: false,
×
78
            write: true, // force write so that our processor gets called
×
79
            status: $operation->getStatus()
×
80
        );
×
81

82
        // save our operation
83
        $request->attributes->set('_api_operation', $swaggerUiOperation);
×
84

NEW
85
        $data = $this->openApiFactory->__invoke([
×
NEW
86
            'base_url' => $request->getBaseUrl() ?: '/',
×
NEW
87
            'filter_tags' => $request->query->all('filter_tags'),
×
NEW
88
        ]);
×
UNCOV
89
        $request->attributes->set('data', $data);
×
90

91
        return $data;
×
92
    }
93
}
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