• 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

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
    ) {
52
        $this->negotiator = $negotiator ?? new Negotiator();
19✔
53
    }
54

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

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

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

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

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

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

103
            return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
13✔
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
    {
117
        if ($this->provider && $this->processor) {
6✔
118
            $context['request'] = $request;
6✔
119
            $operation = new Get(
6✔
120
                class: Documentation::class,
6✔
121
                read: true,
6✔
122
                serialize: true,
6✔
123
                provider: fn () => new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version)
6✔
124
            );
6✔
125

126
            return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
6✔
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