• 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

95.92
/src/Symfony/Bundle/SwaggerUi/SwaggerUiProcessor.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\Exception\RuntimeException;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\OpenApi\OpenApi;
19
use ApiPlatform\OpenApi\Options;
20
use ApiPlatform\OpenApi\Serializer\NormalizeOperationNameTrait;
21
use ApiPlatform\State\ProcessorInterface;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
24
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
25
use Twig\Environment as TwigEnvironment;
26

27
/**
28
 * @internal
29
 */
30
final class SwaggerUiProcessor implements ProcessorInterface
31
{
32
    use NormalizeOperationNameTrait;
33

34
    public function __construct(private readonly ?TwigEnvironment $twig, private readonly UrlGeneratorInterface $urlGenerator, private readonly NormalizerInterface $normalizer, 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)
35
    {
UNCOV
36
        if (null === $this->twig) {
8✔
37
            throw new \RuntimeException('The documentation cannot be displayed since the Twig bundle is not installed. Try running "composer require symfony/twig-bundle".');
×
38
        }
39
    }
40

41
    /**
42
     * @param OpenApi $openApi
43
     */
44
    public function process(mixed $openApi, Operation $operation, array $uriVariables = [], array $context = []): Response
45
    {
UNCOV
46
        $request = $context['request'] ?? null;
8✔
47

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

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

UNCOV
81
        $status = 200;
8✔
UNCOV
82
        $requestedOperation = $request?->attributes->get('_api_requested_operation') ?? null;
8✔
UNCOV
83
        if ($request->isMethodSafe() && $requestedOperation && $requestedOperation->getName()) {
8✔
84
            // TODO: what if the parameter is named something else then `id`?
85
            $swaggerData['id'] = ($request->attributes->get('_api_original_uri_variables') ?? [])['id'] ?? null;
2✔
86
            $swaggerData['queryParameters'] = $request->query->all();
2✔
87

88
            $swaggerData['shortName'] = $requestedOperation->getShortName();
2✔
89
            $swaggerData['operationId'] = $this->normalizeOperationName($requestedOperation->getName());
2✔
90

91
            [$swaggerData['path'], $swaggerData['method']] = $this->getPathAndMethod($swaggerData);
2✔
92
            $status = $requestedOperation->getStatus() ?? $status;
2✔
93
        }
94

UNCOV
95
        return new Response($this->twig->render('@ApiPlatform/SwaggerUi/index.html.twig', $swaggerContext + ['swagger_data' => $swaggerData]), $status);
8✔
96
    }
97

98
    /**
99
     * @param array<string, mixed> $swaggerData
100
     */
101
    private function getPathAndMethod(array $swaggerData): array
102
    {
103
        foreach ($swaggerData['spec']['paths'] as $path => $operations) {
2✔
104
            foreach ($operations as $method => $operation) {
2✔
105
                if (($operation['operationId'] ?? null) === $swaggerData['operationId']) {
2✔
106
                    return [$path, $method];
2✔
107
                }
108
            }
109
        }
110

111
        throw new RuntimeException(\sprintf('The operation "%s" cannot be found in the Swagger specification.', $swaggerData['operationId']));
×
112
    }
113
}
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