File tree Expand file tree Collapse file tree 3 files changed +20
-2
lines changed Expand file tree Collapse file tree 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
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" 
Original file line number Diff line number Diff line change 22
33namespace  Rezzza \SymfonyRestApiJson ;
44
5+ use  Symfony \Component \HttpFoundation \Request ;
56use  Symfony \Component \HttpFoundation \ParameterBag ;
67use  Symfony \Component \HttpKernel \Event \GetResponseEvent ;
78use  Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
9+ use  Symfony \Component \HttpKernel \Exception \UnsupportedMediaTypeHttpException ;
810
911/** 
1012 * Allow to pass JSON raw as request content. 
@@ -31,6 +33,10 @@ public function onKernelRequest(GetResponseEvent $event)
3133            : $ requestgetFormat ($ contentType
3234
3335        if  ($ format'json ' ) {
36+             if  ($ this requestFormatViolateSupportedFormats ($ format$ requestattributes ->get ('_supportedFormats ' , false ))) {
37+                 throw  new  UnsupportedMediaTypeHttpException ("Request body format ' $ format' not supported " );
38+             }
39+ 
3440            return ;
3541        }
3642
@@ -51,4 +57,12 @@ public function onKernelRequest(GetResponseEvent $event)
5157
5258        $ requestrequest  = new  ParameterBag ($ data
5359    }
60+ 
61+     private  function  requestFormatViolateSupportedFormats ($ format$ supportedFormats
62+     {
63+         return  null  !== $ format
64+             && false  !== $ supportedFormats
65+             && false  === in_array ($ format$ supportedFormatstrue )
66+         ;
67+     }
5468}
Original file line number Diff line number Diff line change @@ -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-         $ routesadd ('/echo ' , 'kernel:echoAction ' )->setDefault ('_jsonSchema ' , ['request '  => 'schema.json ' ]);
36+         $ routesadd ('/echo ' , 'kernel:echoAction ' )
37+             ->setDefault ('_jsonSchema ' , ['request '  => 'schema.json ' ])
38+             ->setDefault ('_supportedFormats ' , ['json ' ])
39+         ;
3740        $ routesadd ('/exception ' , 'kernel:exceptionAction ' );
3841    }
3942
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments