File tree 3 files changed +20
-2
lines changed
3 files changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -34,7 +34,8 @@ Feature: Handle json payload
34
34
"name": "Bond"
35
35
}
36
36
"""
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
38
39
39
40
Scenario : Send invalid data
40
41
Given I set "Content-Type" header equal to "application/json"
Original file line number Diff line number Diff line change 2
2
3
3
namespace Rezzza \SymfonyRestApiJson ;
4
4
5
+ use Symfony \Component \HttpFoundation \Request ;
5
6
use Symfony \Component \HttpFoundation \ParameterBag ;
6
7
use Symfony \Component \HttpKernel \Event \GetResponseEvent ;
7
8
use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
9
+ use Symfony \Component \HttpKernel \Exception \UnsupportedMediaTypeHttpException ;
8
10
9
11
/**
10
12
* Allow to pass JSON raw as request content.
@@ -31,6 +33,10 @@ public function onKernelRequest(GetResponseEvent $event)
31
33
: $ request ->getFormat ($ contentType );
32
34
33
35
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
+
34
40
return ;
35
41
}
36
42
@@ -51,4 +57,12 @@ public function onKernelRequest(GetResponseEvent $event)
51
57
52
58
$ request ->request = new ParameterBag ($ data );
53
59
}
60
+
61
+ private function requestFormatViolateSupportedFormats ($ format , $ supportedFormats )
62
+ {
63
+ return null !== $ format
64
+ && false !== $ supportedFormats
65
+ && false === in_array ($ format , $ supportedFormats , true )
66
+ ;
67
+ }
54
68
}
Original file line number Diff line number Diff line change @@ -33,7 +33,10 @@ protected function configureRoutes(RouteCollectionBuilder $routes)
33
33
{
34
34
// kernel is a service that points to this class
35
35
// 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
+ ;
37
40
$ routes ->add ('/exception ' , 'kernel:exceptionAction ' );
38
41
}
39
42
You can’t perform that action at this time.
0 commit comments