Skip to content

Commit 9ad81f8

Browse files
authored
Merge pull request #2892 from dunglas/use-415
A faulty Content-Type should return the 415 status code, not 406
2 parents f1a5fef + 2402747 commit 9ad81f8

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

features/security/validate_incoming_content-types.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ Feature: Validate incoming content type
1111
"""
1212
something
1313
"""
14-
Then the response status code should be 406
14+
Then the response status code should be 415
1515
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
1616
And the JSON node "hydra:description" should be equal to 'The content-type "text/plain" is not supported. Supported MIME types are "application/ld+json", "application/hal+json", "application/vnd.api+json", "application/xml", "text/xml", "application/json", "text/html".'

src/EventListener/DeserializeListener.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use ApiPlatform\Core\Util\RequestAttributesExtractor;
2323
use Symfony\Component\HttpFoundation\Request;
2424
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
25-
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
25+
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
2626
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2727
use Symfony\Component\Serializer\SerializerInterface;
2828

@@ -65,6 +65,8 @@ public function __construct(SerializerInterface $serializer, SerializerContextBu
6565

6666
/**
6767
* Deserializes the data sent in the requested format.
68+
*
69+
* @throws UnsupportedMediaTypeHttpException
6870
*/
6971
public function onKernelRequest(GetResponseEvent $event): void
7072
{
@@ -104,7 +106,7 @@ public function onKernelRequest(GetResponseEvent $event): void
104106
/**
105107
* Extracts the format from the Content-Type header and check that it is supported.
106108
*
107-
* @throws NotAcceptableHttpException
109+
* @throws UnsupportedMediaTypeHttpException
108110
*/
109111
private function getFormat(Request $request): string
110112
{
@@ -113,7 +115,7 @@ private function getFormat(Request $request): string
113115
*/
114116
$contentType = $request->headers->get('CONTENT_TYPE');
115117
if (null === $contentType) {
116-
throw new NotAcceptableHttpException('The "Content-Type" header must exist.');
118+
throw new UnsupportedMediaTypeHttpException('The "Content-Type" header must exist.');
117119
}
118120

119121
$format = $this->formatMatcher->getFormat($contentType);
@@ -125,7 +127,7 @@ private function getFormat(Request $request): string
125127
}
126128
}
127129

128-
throw new NotAcceptableHttpException(sprintf(
130+
throw new UnsupportedMediaTypeHttpException(sprintf(
129131
'The content-type "%s" is not supported. Supported MIME types are "%s".',
130132
$contentType,
131133
implode('", "', $supportedMimeTypes)

tests/EventListener/DeserializeListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use Prophecy\Argument;
2424
use Symfony\Component\HttpFoundation\Request;
2525
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
26-
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
26+
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
2727
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2828
use Symfony\Component\Serializer\SerializerInterface;
2929

@@ -227,7 +227,7 @@ public function testContentNegotiation()
227227

228228
public function testNotSupportedContentType()
229229
{
230-
$this->expectException(NotAcceptableHttpException::class);
230+
$this->expectException(UnsupportedMediaTypeHttpException::class);
231231
$this->expectExceptionMessage('The content-type "application/rdf+xml" is not supported. Supported MIME types are "application/ld+json", "text/xml".');
232232

233233
$eventProphecy = $this->prophesize(GetResponseEvent::class);
@@ -257,7 +257,7 @@ public function testNotSupportedContentType()
257257

258258
public function testNoContentType()
259259
{
260-
$this->expectException(NotAcceptableHttpException::class);
260+
$this->expectException(UnsupportedMediaTypeHttpException::class);
261261
$this->expectExceptionMessage('The "Content-Type" header must exist.');
262262

263263
$eventProphecy = $this->prophesize(GetResponseEvent::class);

0 commit comments

Comments
 (0)