Skip to content

Commit

Permalink
Merge pull request #21 from tyx/feature/handle-415-http
Browse files Browse the repository at this point in the history
Handle 415 Http code through custom _supportedFormats attributes
  • Loading branch information
shouze authored Sep 4, 2017
2 parents ebbe68b + 7c8b4b6 commit da2600d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion features/handle_payload.feature
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Feature: Handle json payload
"name": "Bond"
}
"""
Then the JSON node "name" should not exist
Then the response status code should be 415
And the JSON node "name" should not exist

Scenario: Send invalid data
Given I set "Content-Type" header equal to "application/json"
Expand Down
18 changes: 16 additions & 2 deletions src/JsonBodyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace Rezzza\SymfonyRestApiJson;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;

/**
* Allow to pass JSON raw as request content
* Allow to pass JSON raw as request content.
*/
class JsonBodyListener
{
Expand All @@ -31,6 +33,10 @@ public function onKernelRequest(GetResponseEvent $event)
: $request->getFormat($contentType);

if ($format !== 'json') {
if ($this->requestFormatViolateSupportedFormats($format, $request->attributes->get('_supportedFormats', false))) {
throw new UnsupportedMediaTypeHttpException("Request body format '$format' not supported");
}

return;
}

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

$data = @json_decode($content, true);
if (!is_array($data)) {
throw new BadRequestHttpException('Invalid ' . $format . ' message received');
throw new BadRequestHttpException('Invalid '.$format.' message received');
}

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

$request->request = new ParameterBag($data);
}

private function requestFormatViolateSupportedFormats($format, $supportedFormats)
{
return null !== $format
&& false !== $supportedFormats
&& false === in_array($format, $supportedFormats, true)
;
}
}
7 changes: 5 additions & 2 deletions testapp/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AppKernel extends Kernel
public function registerBundles()
{
return [
new Symfony\Bundle\FrameworkBundle\FrameworkBundle()
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
];
}

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

Expand Down

0 comments on commit da2600d

Please sign in to comment.