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

api-platform / core / 10963383095

20 Sep 2024 05:06PM UTC coverage: 7.834%. Remained the same
10963383095

push

github

soyuka
test(laravel): standard put model should update when it exists

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

342 existing lines in 15 files now uncovered.

12915 of 164861 relevant lines covered (7.83%)

27.03 hits per line

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

92.31
/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();
39✔
53
    }
54

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

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

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

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

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

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

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

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

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

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

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