diff --git a/src/JsonBodyListener.php b/src/JsonBodyListener.php index 43373c0..96deb34 100644 --- a/src/JsonBodyListener.php +++ b/src/JsonBodyListener.php @@ -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); @@ -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); } }