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

api-platform / core / 16531587208

25 Jul 2025 09:05PM UTC coverage: 0.0% (-22.1%) from 22.07%
16531587208

Pull #7225

github

web-flow
Merge 23f449a58 into 02a764950
Pull Request #7225: feat: json streamer

0 of 294 new or added lines in 31 files covered. (0.0%)

11514 existing lines in 375 files now uncovered.

0 of 51976 relevant lines covered (0.0%)

0.0 hits per line

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

0.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
    {
UNCOV
36
        if (null === $this->twig) {
×
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;
×
47

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

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

UNCOV
80
        $status = 200;
×
UNCOV
81
        $requestedOperation = $request?->attributes->get('_api_requested_operation') ?? null;
×
UNCOV
82
        if ($request->isMethodSafe() && $requestedOperation && $requestedOperation->getName()) {
×
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

UNCOV
94
        return new Response($this->twig->render('@ApiPlatform/SwaggerUi/index.html.twig', $swaggerContext + ['swagger_data' => $swaggerData]), $status);
×
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