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

api-platform / core / 7930644932

16 Feb 2024 12:36PM UTC coverage: 66.683% (+0.005%) from 66.678%
7930644932

push

github

web-flow
fix: return null instead of exception for GraphQL Query operation (#6118)

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

53 existing lines in 19 files now uncovered.

16304 of 24450 relevant lines covered (66.68%)

37.74 hits per line

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

93.55
/src/State/Provider/ContentNegotiationProvider.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\State\Provider;
15

16
use ApiPlatform\Metadata\Error as ErrorOperation;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Metadata\Util\ContentNegotiationTrait;
20
use ApiPlatform\State\ProviderInterface;
21
use Negotiation\Negotiator;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
24

25
final class ContentNegotiationProvider implements ProviderInterface
26
{
27
    use ContentNegotiationTrait;
28

29
    /**
30
     * @param array<string, string[]> $formats
31
     * @param array<string, string[]> $errorFormats
32
     */
33
    public function __construct(private readonly ProviderInterface $decorated, ?Negotiator $negotiator = null, private readonly array $formats = [], private readonly array $errorFormats = [])
34
    {
35
        $this->negotiator = $negotiator ?? new Negotiator();
99✔
36
    }
37

38
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
39
    {
40
        if (!($request = $context['request'] ?? null) || !$operation instanceof HttpOperation) {
99✔
41
            return $this->decorated->provide($operation, $uriVariables, $context);
×
42
        }
43

44
        $isErrorOperation = $operation instanceof ErrorOperation;
99✔
45

46
        $formats = $operation->getOutputFormats() ?? ($isErrorOperation ? $this->errorFormats : $this->formats);
99✔
47
        $this->addRequestFormats($request, $formats);
99✔
48
        $request->attributes->set('input_format', $this->getInputFormat($operation, $request));
99✔
49

50
        if (!$isErrorOperation) {
99✔
51
            $request->setRequestFormat($this->getRequestFormat($request, $formats));
95✔
52
        } else {
53
            $request->setRequestFormat($this->getRequestFormat($request, $formats, false));
4✔
54
        }
55

56
        return $this->decorated->provide($operation, $uriVariables, $context);
99✔
57
    }
58

59
    /**
60
     * Adds the supported formats to the request.
61
     *
62
     * This is necessary for {@see Request::getMimeType} and {@see Request::getMimeTypes} to work.
63
     * Note that this replaces default mime types configured at {@see Request::initializeFormats}
64
     *
65
     * @param array<string, string|string[]> $formats
66
     */
67
    private function addRequestFormats(Request $request, array $formats): void
68
    {
69
        foreach ($formats as $format => $mimeTypes) {
99✔
70
            $request->setFormat($format, (array) $mimeTypes);
99✔
71
        }
72
    }
73

74
    /**
75
     * Flattened the list of MIME types.
76
     *
77
     * @param array<string, string|string[]> $formats
78
     *
79
     * @return array<string, string>
80
     */
81
    private function flattenMimeTypes(array $formats): array
82
    {
83
        $flattenedMimeTypes = [];
99✔
84
        foreach ($formats as $format => $mimeTypes) {
99✔
85
            foreach ($mimeTypes as $mimeType) {
99✔
86
                $flattenedMimeTypes[$mimeType] = $format;
99✔
87
            }
88
        }
89

90
        return $flattenedMimeTypes;
99✔
91
    }
92

93
    /**
94
     * Extracts the format from the Content-Type header and check that it is supported.
95
     *
96
     * @throws UnsupportedMediaTypeHttpException
97
     */
98
    private function getInputFormat(HttpOperation $operation, Request $request): ?string
99
    {
100
        $contentType = $request->headers->get('CONTENT_TYPE');
99✔
101
        if (null === $contentType || '' === $contentType) {
99✔
102
            return null;
86✔
103
        }
104

105
        /** @var string $contentType */
106
        $formats = $operation->getInputFormats() ?? [];
13✔
107
        if ($format = $this->getMimeTypeFormat($contentType, $formats)) {
13✔
UNCOV
108
            return $format;
9✔
109
        }
110

111
        $supportedMimeTypes = [];
4✔
112
        foreach ($formats as $mimeTypes) {
4✔
113
            foreach ($mimeTypes as $mimeType) {
4✔
114
                $supportedMimeTypes[] = $mimeType;
4✔
115
            }
116
        }
117

118
        if (!$request->isMethodSafe() && 'DELETE' !== $request->getMethod()) {
4✔
119
            throw new UnsupportedMediaTypeHttpException(sprintf('The content-type "%s" is not supported. Supported MIME types are "%s".', $contentType, implode('", "', $supportedMimeTypes)));
×
120
        }
121

122
        return null;
4✔
123
    }
124
}
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