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
- * Allow to pass JSON raw as request content
12
+ * Allow to pass JSON raw as request content.
11
13
*/
12
14
class JsonBodyListener
13
15
{
@@ -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
@@ -41,7 +47,7 @@ public function onKernelRequest(GetResponseEvent $event)
41
47
42
48
$ data = @json_decode ($ content , true );
43
49
if (!is_array ($ data )) {
44
- throw new BadRequestHttpException ('Invalid ' . $ format . ' message received ' );
50
+ throw new BadRequestHttpException ('Invalid ' . $ format. ' message received ' );
45
51
}
46
52
47
53
$ jsonSchema = $ request ->get ('_jsonSchema ' );
@@ -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
}
0 commit comments