From f7a807473d10b1d619e09500bcf632430354f2ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 11 May 2021 21:30:24 +0200 Subject: [PATCH] send 400 bad request on invalid json --- .../ChillMainBundle/CRUD/Controller/ApiController.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php b/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php index eb84684a9..f4dfc0fc5 100644 --- a/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php +++ b/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php @@ -10,6 +10,8 @@ use Chill\MainBundle\Serializer\Model\Collection; use Chill\MainBundle\Pagination\PaginatorInterface; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Validator\ConstraintViolationListInterface; +use Symfony\Component\Serializer\Exception\NotEncodableValueException; +use Symfony\Component\HttpFoundation\Exception\BadRequestException; class ApiController extends AbstractCRUDController { @@ -122,8 +124,13 @@ class ApiController extends AbstractCRUDController if ($response instanceof Response) { return $response; } + + try { + $entity = $this->deserialize($action, $request, $_format, $entity); + } catch (NotEncodableValueException $e) { + throw new BadRequestException("invalid json", 400, $e); + } - $entity = $this->deserialize($action, $request, $_format, $entity); $errors = $this->validate($action, $request, $_format, $entity); $response = $this->onAfterValidation($action, $request, $_format, $entity, $errors);