Skip to content

Commit bec147b

Browse files
authored
fix(state): do not check content type if no input (#6794)
1 parent a98332d commit bec147b

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

src/State/Provider/ContentNegotiationProvider.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ private function flattenMimeTypes(array $formats): array
9797
*/
9898
private function getInputFormat(HttpOperation $operation, Request $request): ?string
9999
{
100+
if (
101+
false === ($input = $operation->getInput())
102+
|| (\is_array($input) && null === $input['class'])
103+
|| false === $operation->canDeserialize()
104+
) {
105+
return null;
106+
}
107+
100108
$contentType = $request->headers->get('CONTENT_TYPE');
101109
if (null === $contentType || '' === $contentType) {
102110
return null;
@@ -108,14 +116,14 @@ private function getInputFormat(HttpOperation $operation, Request $request): ?st
108116
return $format;
109117
}
110118

111-
$supportedMimeTypes = [];
112-
foreach ($formats as $mimeTypes) {
113-
foreach ($mimeTypes as $mimeType) {
114-
$supportedMimeTypes[] = $mimeType;
119+
if (!$request->isMethodSafe() && 'DELETE' !== $request->getMethod()) {
120+
$supportedMimeTypes = [];
121+
foreach ($formats as $mimeTypes) {
122+
foreach ($mimeTypes as $mimeType) {
123+
$supportedMimeTypes[] = $mimeType;
124+
}
115125
}
116-
}
117126

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

tests/State/Provider/ContentNegotiationProviderTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Prophecy\Argument;
2222
use Prophecy\PhpUnit\ProphecyTrait;
2323
use Symfony\Component\HttpFoundation\Request;
24+
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
2425

2526
class ContentNegotiationProviderTest extends TestCase
2627
{
@@ -60,4 +61,63 @@ public function testRequestWithEmptyContentType(): void
6061

6162
$this->assertSame($expectedResult, $result);
6263
}
64+
65+
public function testRequestWhenNoInput(): void
66+
{
67+
$expectedResult = new \stdClass();
68+
69+
$decorated = $this->prophesize(ProviderInterface::class);
70+
$decorated->provide(Argument::cetera())->willReturn($expectedResult);
71+
72+
$negotiator = new Negotiator();
73+
$formats = ['jsonld' => ['application/ld+json']];
74+
$errorFormats = ['jsonld' => ['application/ld+json']];
75+
76+
$provider = new ContentNegotiationProvider($decorated->reveal(), $negotiator, $formats, $errorFormats);
77+
78+
$request = new Request(
79+
server: [
80+
'REQUEST_METHOD' => 'POST',
81+
'REQUEST_URI' => '/',
82+
'CONTENT_TYPE' => 'some-not-supported/content-type',
83+
],
84+
content: ''
85+
);
86+
87+
$operation = new Post();
88+
$operation = $operation->withDeserialize(false);
89+
$context = ['request' => $request];
90+
91+
$result = $provider->provide($operation, [], $context);
92+
93+
$this->assertSame($expectedResult, $result);
94+
}
95+
96+
public function testRequestWithInput(): void
97+
{
98+
$this->expectException(UnsupportedMediaTypeHttpException::class);
99+
100+
$decorated = $this->prophesize(ProviderInterface::class);
101+
102+
$negotiator = new Negotiator();
103+
$formats = ['jsonld' => ['application/ld+json']];
104+
$errorFormats = ['jsonld' => ['application/ld+json']];
105+
106+
$provider = new ContentNegotiationProvider($decorated->reveal(), $negotiator, $formats, $errorFormats);
107+
108+
$request = new Request(
109+
server: [
110+
'REQUEST_METHOD' => 'POST',
111+
'REQUEST_URI' => '/',
112+
'CONTENT_TYPE' => 'some-not-supported/content-type',
113+
],
114+
content: ''
115+
);
116+
117+
$operation = new Post();
118+
$operation = $operation->withDeserialize();
119+
$context = ['request' => $request];
120+
121+
$provider->provide($operation, [], $context);
122+
}
63123
}

0 commit comments

Comments
 (0)