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

api-platform / core / 14967095168

12 May 2025 08:08AM UTC coverage: 22.155% (+13.7%) from 8.457%
14967095168

Pull #7135

github

web-flow
Merge 574a2b863 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

1 of 2 new or added lines in 2 files covered. (50.0%)

120 existing lines in 12 files now uncovered.

10846 of 48956 relevant lines covered (22.15%)

10.12 hits per line

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

87.88
/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 = null, ?Negotiator $negotiator = null, private readonly array $formats = [], private readonly array $errorFormats = [])
34
    {
35
        $this->negotiator = $negotiator ?? new Negotiator();
219✔
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) {
219✔
41
            return $this->decorated?->provide($operation, $uriVariables, $context);
×
42
        }
43

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

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

51
        return $this->decorated?->provide($operation, $uriVariables, $context);
218✔
52
    }
53

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

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

85
        return $flattenedMimeTypes;
218✔
86
    }
87

88
    /**
89
     * Extracts the format from the Content-Type header and check that it is supported.
90
     *
91
     * @throws UnsupportedMediaTypeHttpException
92
     */
93
    private function getInputFormat(HttpOperation $operation, Request $request): ?string
94
    {
95
        if (
96
            false === ($input = $operation->getInput())
219✔
97
            || (\is_array($input) && null === $input['class'])
219✔
98
            || false === $operation->canDeserialize()
219✔
99
        ) {
100
            return null;
199✔
101
        }
102

103
        $contentType = $request->headers->get('CONTENT_TYPE');
22✔
104
        if (null === $contentType || '' === $contentType) {
22✔
105
            return null;
14✔
106
        }
107

108
        /** @var string $contentType */
109
        $formats = $operation->getInputFormats() ?? [];
8✔
110
        if ($format = $this->getMimeTypeFormat($contentType, $formats)) {
8✔
111
            return $format;
7✔
112
        }
113

114
        if (!$request->isMethodSafe() && 'DELETE' !== $request->getMethod()) {
1✔
115
            $supportedMimeTypes = [];
1✔
116
            foreach ($formats as $mimeTypes) {
1✔
117
                foreach ($mimeTypes as $mimeType) {
×
118
                    $supportedMimeTypes[] = $mimeType;
×
119
                }
120
            }
121

122
            throw new UnsupportedMediaTypeHttpException(\sprintf('The content-type "%s" is not supported. Supported MIME types are "%s".', $contentType, implode('", "', $supportedMimeTypes)));
1✔
123
        }
124

UNCOV
125
        return null;
×
126
    }
127
}
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