• 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

94.23
/src/Symfony/Bundle/SwaggerUi/SwaggerUiAction.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\Bundle\SwaggerUi;
15

16
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
17
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
18
use ApiPlatform\OpenApi\Options;
19
use ApiPlatform\OpenApi\Serializer\NormalizeOperationNameTrait;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
23
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
24
use Twig\Environment as TwigEnvironment;
25

26
/**
27
 * Displays the swaggerui interface.
28
 *
29
 * @deprecated use ApiPlatform\Symfony\Action\DocumentationAction instead
30
 *
31
 * @author Antoine Bluchet <soyuka@gmail.com>
32
 */
33
final class SwaggerUiAction
34
{
35
    use NormalizeOperationNameTrait;
36

37
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly ?TwigEnvironment $twig, private readonly UrlGeneratorInterface $urlGenerator, private readonly NormalizerInterface $normalizer, private readonly OpenApiFactoryInterface $openApiFactory, private readonly Options $openApiOptions, private readonly SwaggerUiContext $swaggerUiContext, private readonly array $formats = [], private readonly ?string $oauthClientId = null, private readonly ?string $oauthClientSecret = null, private readonly bool $oauthPkce = false)
38
    {
UNCOV
39
        if (null === $this->twig) {
5✔
40
            throw new \RuntimeException('The documentation cannot be displayed since the Twig bundle is not installed. Try running "composer require symfony/twig-bundle".');
×
41
        }
42
    }
43

44
    public function __invoke(Request $request): Response
45
    {
UNCOV
46
        $openApi = $this->openApiFactory->__invoke(['base_url' => $request->getBaseUrl() ?: '/']);
5✔
47

UNCOV
48
        foreach ($request->attributes->get('_api_exception_swagger_data') ?? [] as $key => $value) {
5✔
49
            $request->attributes->set($key, $value);
×
50
        }
51

UNCOV
52
        $swaggerContext = [
5✔
UNCOV
53
            'formats' => $this->formats,
5✔
UNCOV
54
            'title' => $openApi->getInfo()->getTitle(),
5✔
UNCOV
55
            'description' => $openApi->getInfo()->getDescription(),
5✔
UNCOV
56
            'showWebby' => $this->swaggerUiContext->isWebbyShown(),
5✔
UNCOV
57
            'swaggerUiEnabled' => $this->swaggerUiContext->isSwaggerUiEnabled(),
5✔
UNCOV
58
            'reDocEnabled' => $this->swaggerUiContext->isRedocEnabled(),
5✔
UNCOV
59
            'graphQlEnabled' => $this->swaggerUiContext->isGraphQlEnabled(),
5✔
UNCOV
60
            'graphiQlEnabled' => $this->swaggerUiContext->isGraphiQlEnabled(),
5✔
UNCOV
61
            'graphQlPlaygroundEnabled' => $this->swaggerUiContext->isGraphQlPlaygroundEnabled(),
5✔
UNCOV
62
            'assetPackage' => $this->swaggerUiContext->getAssetPackage(),
5✔
UNCOV
63
            'originalRoute' => $request->attributes->get('_api_original_route', $request->attributes->get('_route')),
5✔
UNCOV
64
            'originalRouteParams' => $request->attributes->get('_api_original_route_params', $request->attributes->get('_route_params', [])),
5✔
UNCOV
65
        ];
5✔
66

UNCOV
67
        $swaggerData = [
5✔
UNCOV
68
            'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']),
5✔
UNCOV
69
            'spec' => $this->normalizer->normalize($openApi, 'json', []),
5✔
UNCOV
70
            'oauth' => [
5✔
UNCOV
71
                'enabled' => $this->openApiOptions->getOAuthEnabled(),
5✔
UNCOV
72
                'type' => $this->openApiOptions->getOAuthType(),
5✔
UNCOV
73
                'flow' => $this->openApiOptions->getOAuthFlow(),
5✔
UNCOV
74
                'tokenUrl' => $this->openApiOptions->getOAuthTokenUrl(),
5✔
UNCOV
75
                'authorizationUrl' => $this->openApiOptions->getOAuthAuthorizationUrl(),
5✔
UNCOV
76
                'scopes' => $this->openApiOptions->getOAuthScopes(),
5✔
UNCOV
77
                'clientId' => $this->oauthClientId,
5✔
UNCOV
78
                'clientSecret' => $this->oauthClientSecret,
5✔
UNCOV
79
                'pkce' => $this->oauthPkce,
5✔
UNCOV
80
            ],
5✔
UNCOV
81
            'extraConfiguration' => $this->swaggerUiContext->getExtraConfiguration(),
5✔
UNCOV
82
            'persistAuthorization' => $this->openApiOptions->hasPersistAuthorization(),
5✔
UNCOV
83
        ];
5✔
84

UNCOV
85
        $originalRouteParams = $request->attributes->get('_api_original_route_params') ?? [];
5✔
UNCOV
86
        $resourceClass = $originalRouteParams['_api_resource_class'] ?? $request->attributes->get('_api_resource_class');
5✔
87

UNCOV
88
        if ($request->isMethodSafe() && $resourceClass) {
5✔
UNCOV
89
            $swaggerData['id'] = $request->attributes->get('id');
3✔
UNCOV
90
            $swaggerData['queryParameters'] = $request->query->all();
3✔
91

UNCOV
92
            $metadata = $this->resourceMetadataFactory->create($resourceClass)->getOperation($originalRouteParams['_api_operation_name'] ?? $request->attributes->get('_api_operation_name'));
3✔
93

UNCOV
94
            $swaggerData['shortName'] = $metadata->getShortName();
3✔
UNCOV
95
            $swaggerData['operationId'] = $this->normalizeOperationName($metadata->getName());
3✔
96

UNCOV
97
            if ($data = $this->getPathAndMethod($swaggerData)) {
3✔
UNCOV
98
                [$swaggerData['path'], $swaggerData['method']] = $data;
3✔
99
            }
100
        }
101

UNCOV
102
        return new Response($this->twig->render('@ApiPlatform/SwaggerUi/index.html.twig', $swaggerContext + ['swagger_data' => $swaggerData]));
5✔
103
    }
104

105
    private function getPathAndMethod(array $swaggerData): ?array
106
    {
UNCOV
107
        foreach ($swaggerData['spec']['paths'] as $path => $operations) {
3✔
UNCOV
108
            foreach ($operations as $method => $operation) {
3✔
UNCOV
109
                if (($operation['operationId'] ?? null) === $swaggerData['operationId']) {
3✔
UNCOV
110
                    return [$path, $method];
3✔
111
                }
112
            }
113
        }
114

115
        return null;
×
116
    }
117
}
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