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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

5.17 hits per line

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

75.0
/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) {
6✔
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;
6✔
47

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

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

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

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

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

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

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

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