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

api-platform / core / 10740459985

06 Sep 2024 02:47PM UTC coverage: 6.92% (-0.2%) from 7.159%
10740459985

push

github

web-flow
fix(laravel): docs _format and open swagger ui (#6595)

0 of 90 new or added lines in 11 files covered. (0.0%)

1910 existing lines in 69 files now uncovered.

11347 of 163980 relevant lines covered (6.92%)

14.18 hits per line

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

0.0
/src/Laravel/Controller/DocumentationController.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\Laravel\Controller;
15

16
use ApiPlatform\Documentation\Documentation;
17
use ApiPlatform\Metadata\Get;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
19
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
20
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
21
use ApiPlatform\OpenApi\OpenApi;
22
use ApiPlatform\OpenApi\Serializer\ApiGatewayNormalizer;
23
use ApiPlatform\OpenApi\Serializer\LegacyOpenApiNormalizer;
24
use ApiPlatform\OpenApi\Serializer\OpenApiNormalizer;
25
use ApiPlatform\State\ProcessorInterface;
26
use ApiPlatform\State\ProviderInterface;
27
use Negotiation\Negotiator;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\Response;
30

31
/**
32
 * Generates the API documentation.
33
 *
34
 * @author Amrouche Hamza <hamza.simperfit@gmail.com>
35
 */
36
final class DocumentationController
37
{
38
    use ContentNegotiationTrait;
39

40
    /**
41
     * @param array<string, string[]>             $documentationFormats
42
     * @param ProviderInterface<object>           $provider
43
     * @param ProcessorInterface<mixed, Response> $processor
44
     */
45
    public function __construct(
46
        private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory,
47
        private readonly string $title = '',
48
        private readonly string $description = '',
49
        private readonly string $version = '',
50
        private readonly ?OpenApiFactoryInterface $openApiFactory = null,
51
        private readonly ?ProviderInterface $provider = null,
52
        private readonly ?ProcessorInterface $processor = null,
53
        ?Negotiator $negotiator = null,
54
        private readonly array $documentationFormats = [OpenApiNormalizer::JSON_FORMAT => ['application/vnd.openapi+json'], OpenApiNormalizer::FORMAT => ['application/json']],
55
        private readonly bool $swaggerUiEnabled = true,
56
    ) {
NEW
57
        $this->negotiator = $negotiator ?? new Negotiator();
×
58
    }
59

60
    public function __invoke(Request $request): Response
61
    {
NEW
62
        $context = [
×
NEW
63
            'api_gateway' => $request->query->getBoolean(ApiGatewayNormalizer::API_GATEWAY),
×
NEW
64
            'base_url' => $request->getBaseUrl(),
×
NEW
65
            'spec_version' => (string) $request->query->get(LegacyOpenApiNormalizer::SPEC_VERSION),
×
NEW
66
        ];
×
NEW
67
        $request->attributes->set('_api_normalization_context', $request->attributes->get('_api_normalization_context', []) + $context);
×
68
        // We want to find the format early on, this code is also executed later on by the ContentNegotiationProvider.
NEW
69
        $this->addRequestFormats($request, $this->documentationFormats);
×
NEW
70
        $format = $this->getRequestFormat($request, $this->documentationFormats);
×
71

NEW
72
        if ('html' === $format || OpenApiNormalizer::FORMAT === $format || OpenApiNormalizer::JSON_FORMAT === $format || OpenApiNormalizer::YAML_FORMAT === $format) {
×
NEW
73
            return $this->getOpenApiDocumentation($context, $format, $request);
×
74
        }
75

NEW
76
        return $this->getHydraDocumentation($context, $request);
×
77
    }
78

79
    /**
80
     * @param array<string,mixed> $context
81
     */
82
    private function getOpenApiDocumentation(array $context, string $format, Request $request): Response
83
    {
NEW
84
        $context['request'] = $request;
×
NEW
85
        $operation = new Get(
×
NEW
86
            class: OpenApi::class,
×
NEW
87
            read: true,
×
NEW
88
            serialize: true,
×
NEW
89
            provider: fn () => $this->openApiFactory->__invoke($context),
×
NEW
90
            normalizationContext: [
×
NEW
91
                ApiGatewayNormalizer::API_GATEWAY => $context['api_gateway'] ?? null,
×
NEW
92
                LegacyOpenApiNormalizer::SPEC_VERSION => $context['spec_version'] ?? null,
×
NEW
93
            ],
×
NEW
94
            outputFormats: $this->documentationFormats
×
NEW
95
        );
×
96

NEW
97
        if ('html' === $format && $this->swaggerUiEnabled) {
×
NEW
98
            $operation = $operation->withProcessor('api_platform.swagger_ui.processor')->withWrite(true);
×
99
        }
100

NEW
101
        return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
×
102
    }
103

104
    /**
105
     * TODO: the logic behind the Hydra Documentation is done in a ApiPlatform\Hydra\Serializer\DocumentationNormalizer.
106
     * We should transform this to a provider, it'd improve performances also by a bit.
107
     *
108
     * @param array<string,mixed> $context
109
     */
110
    private function getHydraDocumentation(array $context, Request $request): Response
111
    {
NEW
112
        $context['request'] = $request;
×
NEW
113
        $operation = new Get(
×
NEW
114
            class: Documentation::class,
×
NEW
115
            read: true,
×
NEW
116
            serialize: true,
×
NEW
117
            provider: fn () => new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version)
×
NEW
118
        );
×
119

NEW
120
        return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
×
121
    }
122
}
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