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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 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
    {
36
        if (null === $this->twig) {
10✔
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;
10✔
47

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

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

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

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

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

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

98
    /**
99
     * @param array<string, mixed> $swaggerData
100
     */
101
    private function getPathAndMethod(array $swaggerData): array
102
    {
UNCOV
103
        foreach ($swaggerData['spec']['paths'] as $path => $operations) {
2✔
UNCOV
104
            foreach ($operations as $method => $operation) {
2✔
UNCOV
105
                if (($operation['operationId'] ?? null) === $swaggerData['operationId']) {
2✔
UNCOV
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