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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

92.5
/src/Symfony/Action/DocumentationAction.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\Action;
15

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

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

41
    public function __construct(
42
        private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory,
43
        private readonly string $title = '',
44
        private readonly string $description = '',
45
        private readonly string $version = '',
46
        private readonly ?OpenApiFactoryInterface $openApiFactory = null,
47
        private readonly ?ProviderInterface $provider = null,
48
        private readonly ?ProcessorInterface $processor = null,
49
        ?Negotiator $negotiator = null,
50
        private readonly array $documentationFormats = [OpenApiNormalizer::JSON_FORMAT => ['application/vnd.openapi+json'], OpenApiNormalizer::FORMAT => ['application/json']],
51
    ) {
UNCOV
52
        $this->negotiator = $negotiator ?? new Negotiator();
15✔
53
    }
54

55
    /**
56
     * @return DocumentationInterface|OpenApi|Response
57
     */
58
    public function __invoke(?Request $request = null)
59
    {
UNCOV
60
        if (null === $request) {
15✔
61
            return new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version);
×
62
        }
63

UNCOV
64
        $context = [
15✔
UNCOV
65
            'api_gateway' => $request->query->getBoolean(ApiGatewayNormalizer::API_GATEWAY),
15✔
UNCOV
66
            'base_url' => $request->getBaseUrl(),
15✔
UNCOV
67
            'spec_version' => (string) $request->query->get(LegacyOpenApiNormalizer::SPEC_VERSION),
15✔
UNCOV
68
            'filter_tags' => $request->query->all('filter_tags'),
15✔
UNCOV
69
        ];
15✔
UNCOV
70
        $request->attributes->set('_api_normalization_context', $request->attributes->get('_api_normalization_context', []) + $context);
15✔
UNCOV
71
        $this->addRequestFormats($request, $this->documentationFormats);
15✔
UNCOV
72
        $format = $this->getRequestFormat($request, $this->documentationFormats);
15✔
73

UNCOV
74
        if (null !== $this->openApiFactory && ('html' === $format || OpenApiNormalizer::FORMAT === $format || OpenApiNormalizer::JSON_FORMAT === $format || OpenApiNormalizer::YAML_FORMAT === $format)) {
15✔
UNCOV
75
            return $this->getOpenApiDocumentation($context, $format, $request);
10✔
76
        }
77

UNCOV
78
        return $this->getHydraDocumentation($context, $request);
5✔
79
    }
80

81
    /**
82
     * @param array<string,mixed> $context
83
     */
84
    private function getOpenApiDocumentation(array $context, string $format, Request $request): OpenApi|Response
85
    {
UNCOV
86
        if ($this->provider && $this->processor) {
10✔
UNCOV
87
            $context['request'] = $request;
10✔
UNCOV
88
            $operation = new Get(
10✔
UNCOV
89
                class: OpenApi::class,
10✔
UNCOV
90
                read: true,
10✔
UNCOV
91
                serialize: true,
10✔
UNCOV
92
                provider: 'api_platform.openapi.provider',
10✔
UNCOV
93
                outputFormats: $this->documentationFormats
10✔
UNCOV
94
            );
10✔
95

UNCOV
96
            if ('html' === $format) {
10✔
97
                $operation = $operation->withProcessor('api_platform.swagger_ui.processor')->withWrite(true);
1✔
98
            }
UNCOV
99
            if ('json' === $format) {
10✔
100
                trigger_deprecation('api-platform/core', '3.2', 'The "json" format is too broad, use "jsonopenapi" instead.');
2✔
101
            }
102

UNCOV
103
            return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
10✔
104
        }
105

106
        return $this->openApiFactory->__invoke($context);
×
107
    }
108

109
    /**
110
     * TODO: the logic behind the Hydra Documentation is done in a ApiPlatform\Hydra\Serializer\DocumentationNormalizer.
111
     * We should transform this to a provider, it'd improve performances also by a bit.
112
     *
113
     * @param array<string,mixed> $context
114
     */
115
    private function getHydraDocumentation(array $context, Request $request): DocumentationInterface|Response
116
    {
UNCOV
117
        if ($this->provider && $this->processor) {
5✔
UNCOV
118
            $context['request'] = $request;
5✔
UNCOV
119
            $operation = new Get(
5✔
UNCOV
120
                class: Documentation::class,
5✔
UNCOV
121
                read: true,
5✔
UNCOV
122
                serialize: true,
5✔
UNCOV
123
                provider: fn () => new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version)
5✔
UNCOV
124
            );
5✔
125

UNCOV
126
            return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
5✔
127
        }
128

129
        return new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version);
×
130
    }
131
}
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