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

api-platform / core / 8100432467

29 Feb 2024 05:53PM UTC coverage: 56.922% (-7.0%) from 63.915%
8100432467

push

github

web-flow
fix: components split dependencies (#6186)

19 of 61 new or added lines in 2 files covered. (31.15%)

19 existing lines in 1 file now uncovered.

9510 of 16707 relevant lines covered (56.92%)

41.0 hits per line

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

0.0
/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
    ) {
NEW
52
        $this->negotiator = $negotiator ?? new Negotiator();
×
53
    }
54

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

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

NEW
72
        if (null !== $this->openApiFactory && ('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): OpenApi|Response
83
    {
NEW
84
        if ($this->provider && $this->processor) {
×
NEW
85
            $context['request'] = $request;
×
NEW
86
            $operation = new Get(
×
NEW
87
                class: OpenApi::class,
×
NEW
88
                read: true,
×
NEW
89
                serialize: true,
×
NEW
90
                provider: fn () => $this->openApiFactory->__invoke($context),
×
NEW
91
                normalizationContext: [
×
NEW
92
                    ApiGatewayNormalizer::API_GATEWAY => $context['api_gateway'] ?? null,
×
NEW
93
                    LegacyOpenApiNormalizer::SPEC_VERSION => $context['spec_version'] ?? null,
×
NEW
94
                ],
×
NEW
95
                outputFormats: $this->documentationFormats
×
NEW
96
            );
×
97

NEW
98
            if ('html' === $format) {
×
NEW
99
                $operation = $operation->withProcessor('api_platform.swagger_ui.processor')->withWrite(true);
×
100
            }
NEW
101
            if ('json' === $format) {
×
NEW
102
                trigger_deprecation('api-platform/core', '3.2', 'The "json" format is too broad, use "jsonopenapi" instead.');
×
103
            }
104

NEW
105
            return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
×
106
        }
107

NEW
108
        return $this->openApiFactory->__invoke($context);
×
109
    }
110

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

NEW
128
            return $this->processor->process($this->provider->provide($operation, [], $context), $operation, [], $context);
×
129
        }
130

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