Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle 415 Http code through custom _supportedFormats attributes #21

Merged
merged 2 commits into from
Sep 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean false !== is_array($supportedFormats)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nop, false is the default value I set when keys is missing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed 👍

&& 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