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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

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

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 hits per line

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

85.71
/src/Documentation/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\Documentation\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\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 DocumentationAction
37
{
38
    use ContentNegotiationTrait;
39

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

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

63
        $context = ['api_gateway' => $request->query->getBoolean(ApiGatewayNormalizer::API_GATEWAY), 'base_url' => $request->getBaseUrl()];
33✔
64
        $request->attributes->set('_api_normalization_context', $request->attributes->get('_api_normalization_context', []) + $context);
33✔
65
        $format = $this->getRequestFormat($request, $this->documentationFormats);
33✔
66

67
        if (null !== $this->openApiFactory && ('html' === $format || OpenApiNormalizer::FORMAT === $format || OpenApiNormalizer::JSON_FORMAT === $format || OpenApiNormalizer::YAML_FORMAT === $format)) {
33✔
68
            return $this->getOpenApiDocumentation($context, $format, $request);
21✔
69
        }
70

71
        return $this->getHydraDocumentation($context, $request);
12✔
72
    }
73

74
    /**
75
     * @param array<string,mixed> $context
76
     */
77
    private function getOpenApiDocumentation(array $context, string $format, Request $request): OpenApi|Response
78
    {
79
        if ($this->provider && $this->processor) {
21✔
80
            $context['request'] = $request;
21✔
81
            $operation = new Get(class: OpenApi::class, read: true, serialize: true, provider: fn () => $this->openApiFactory->__invoke($context), normalizationContext: [ApiGatewayNormalizer::API_GATEWAY => $context['api_gateway'] ?? null], outputFormats: $this->documentationFormats);
21✔
82
            if ('html' === $format) {
21✔
83
                $operation = $operation->withProcessor('api_platform.swagger_ui.processor')->withWrite(true);
×
84
            }
85
            if ('json' === $format) {
21✔
86
                trigger_deprecation('api-platform/core', '3.2', 'The "json" format is too broad, use "jsonopenapi" instead.');
15✔
87
            }
88

89
            return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
21✔
90
        }
91

UNCOV
92
        return $this->openApiFactory->__invoke($context);
×
93
    }
94

95
    /**
96
     * TODO: the logic behind the Hydra Documentation is done in a ApiPlatform\Hydra\Serializer\DocumentationNormalizer.
97
     * We should transform this to a provider, it'd improve performances also by a bit.
98
     *
99
     * @param array<string,mixed> $context
100
     */
101
    private function getHydraDocumentation(array $context, Request $request): DocumentationInterface|Response
102
    {
103
        if ($this->provider && $this->processor) {
12✔
104
            $context['request'] = $request;
12✔
105
            $operation = new Get(
12✔
106
                class: Documentation::class,
12✔
107
                read: true,
12✔
108
                serialize: true,
12✔
109
                provider: fn () => new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version)
12✔
110
            );
12✔
111

112
            return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
12✔
113
        }
114

UNCOV
115
        return new Documentation($this->resourceNameCollectionFactory->create(), $this->title, $this->description, $this->version);
×
116
    }
117
}
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