Skip to content

Commit

Permalink
Merge pull request #20 from shouze/fix/request-content-edge-case
Browse files Browse the repository at this point in the history
🐛 Fix request content check edge case
  • Loading branch information
tyx authored Aug 31, 2017
2 parents 6ede6f8 + 93a8e76 commit ebbe68b
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions src/JsonBodyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,11 @@ public function __construct(PayloadValidator $payloadValidator)
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$method = $request->getMethod();

if (count($request->request->all())
|| !in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE', 'LINK', 'UNLINK'])
) {
if (!in_array($request->getMethod(), ['POST', 'PUT', 'PATCH', 'DELETE', 'LINK', 'UNLINK'])) {
return;
}

$contentType = $request->headers->get('Content-Type');

$format = null === $contentType
? $request->getRequestFormat()
: $request->getFormat($contentType);
Expand All @@ -40,20 +35,20 @@ public function onKernelRequest(GetResponseEvent $event)
}

$content = $request->getContent();
if (empty($content)) {
return;
}

if (!empty($content)) {
$data = @json_decode($content, true);

if (!is_array($data)) {
throw new BadRequestHttpException('Invalid ' . $format . ' message received');
}

$jsonSchema = $request->get('_jsonSchema');
if (is_array($jsonSchema) && array_key_exists('request', $jsonSchema)) {
$this->payloadValidator->validate($content, $jsonSchema['request']);
}
$data = @json_decode($content, true);
if (!is_array($data)) {
throw new BadRequestHttpException('Invalid ' . $format . ' message received');
}

$request->request = new ParameterBag($data);
$jsonSchema = $request->get('_jsonSchema');
if (is_array($jsonSchema) && array_key_exists('request', $jsonSchema)) {
$this->payloadValidator->validate($content, $jsonSchema['request']);
}

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

0 comments on commit ebbe68b

Please sign in to comment.