Skip to content

Commit da2600d

Browse files
authored
Merge pull request #21 from tyx/feature/handle-415-http
Handle 415 Http code through custom _supportedFormats attributes
2 parents ebbe68b + 7c8b4b6 commit da2600d

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

features/handle_payload.feature

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Feature: Handle json payload
3434
"name": "Bond"
3535
}
3636
"""
37-
Then the JSON node "name" should not exist
37+
Then the response status code should be 415
38+
And the JSON node "name" should not exist
3839

3940
Scenario: Send invalid data
4041
Given I set "Content-Type" header equal to "application/json"

src/JsonBodyListener.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Rezzza\SymfonyRestApiJson;
44

5+
use Symfony\Component\HttpFoundation\Request;
56
use Symfony\Component\HttpFoundation\ParameterBag;
67
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
78
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
9+
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
810

911
/**
10-
* Allow to pass JSON raw as request content
12+
* Allow to pass JSON raw as request content.
1113
*/
1214
class JsonBodyListener
1315
{
@@ -31,6 +33,10 @@ public function onKernelRequest(GetResponseEvent $event)
3133
: $request->getFormat($contentType);
3234

3335
if ($format !== 'json') {
36+
if ($this->requestFormatViolateSupportedFormats($format, $request->attributes->get('_supportedFormats', false))) {
37+
throw new UnsupportedMediaTypeHttpException("Request body format '$format' not supported");
38+
}
39+
3440
return;
3541
}
3642

@@ -41,7 +47,7 @@ public function onKernelRequest(GetResponseEvent $event)
4147

4248
$data = @json_decode($content, true);
4349
if (!is_array($data)) {
44-
throw new BadRequestHttpException('Invalid ' . $format . ' message received');
50+
throw new BadRequestHttpException('Invalid '.$format.' message received');
4551
}
4652

4753
$jsonSchema = $request->get('_jsonSchema');
@@ -51,4 +57,12 @@ public function onKernelRequest(GetResponseEvent $event)
5157

5258
$request->request = new ParameterBag($data);
5359
}
60+
61+
private function requestFormatViolateSupportedFormats($format, $supportedFormats)
62+
{
63+
return null !== $format
64+
&& false !== $supportedFormats
65+
&& false === in_array($format, $supportedFormats, true)
66+
;
67+
}
5468
}

testapp/index.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AppKernel extends Kernel
1818
public function registerBundles()
1919
{
2020
return [
21-
new Symfony\Bundle\FrameworkBundle\FrameworkBundle()
21+
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
2222
];
2323
}
2424

@@ -33,7 +33,10 @@ protected function configureRoutes(RouteCollectionBuilder $routes)
3333
{
3434
// kernel is a service that points to this class
3535
// optional 3rd argument is the route name
36-
$routes->add('/echo', 'kernel:echoAction')->setDefault('_jsonSchema', ['request' => 'schema.json']);
36+
$routes->add('/echo', 'kernel:echoAction')
37+
->setDefault('_jsonSchema', ['request' => 'schema.json'])
38+
->setDefault('_supportedFormats', ['json'])
39+
;
3740
$routes->add('/exception', 'kernel:exceptionAction');
3841
}
3942

0 commit comments

Comments
 (0)