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

api-platform / core / 10027319583

21 Jul 2024 09:36AM UTC coverage: 7.847%. Remained the same
10027319583

push

github

web-flow
ci: fix naming of Windows Behat matrix line (#6489)

12688 of 161690 relevant lines covered (7.85%)

26.88 hits per line

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

95.83
/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
    {
36
        if (null === $this->twig) {
21✔
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
    {
46
        $request = $context['request'] ?? null;
21✔
47

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

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

80
        $status = 200;
21✔
81
        $requestedOperation = $request?->attributes->get('_api_requested_operation') ?? null;
21✔
82
        if ($request->isMethodSafe() && $requestedOperation && $requestedOperation->getName()) {
21✔
83
            $swaggerData['id'] = $request->get('id');
3✔
84
            $swaggerData['queryParameters'] = $request->query->all();
3✔
85

86
            $swaggerData['shortName'] = $requestedOperation->getShortName();
3✔
87
            $swaggerData['operationId'] = $this->normalizeOperationName($requestedOperation->getName());
3✔
88

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

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

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

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