From 7d0ebe87ad9bbce11fc94d03c55568db2b992ad5 Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Mon, 4 Aug 2025 13:48:08 +0200 Subject: [PATCH] Log raw JSON when fetching the JSON content from a request fails and improve implementation --- CHANGELOG.md | 6 ++++++ src/Controller/BaseController.php | 28 +++++++++------------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 701a5e7..53bd67a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +3.3.1 (unreleased) +===== + +* (improvement) Log raw JSON when fetching the JSON content from a request fails. + + 3.3.0 ===== diff --git a/src/Controller/BaseController.php b/src/Controller/BaseController.php index 054d8eb..648334c 100644 --- a/src/Controller/BaseController.php +++ b/src/Controller/BaseController.php @@ -5,6 +5,7 @@ use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\FormInterface; +use Symfony\Component\HttpFoundation\Exception\JsonException; use Symfony\Component\HttpFoundation\Request; use Torr\Rad\Exception\Request\InvalidJsonRequestException; use Torr\Rad\Form\FormErrorNormalizer; @@ -72,29 +73,18 @@ public function fetchJsonRequestBody (Request $request, bool $allowInvalid = fal throw new InvalidJsonRequestException("Expected JSON request content type.", 415); } - $raw = trim((string) $request->getContent()); - - if ("" === $raw) - { - return []; - } - try { - $data = json_decode($raw, true, 512, \JSON_THROW_ON_ERROR); - - if (!\is_array($data)) - { - throw new InvalidJsonRequestException( - \sprintf("Invalid top level type in JSON payload. Must be array, is %s", \gettype($data)), - 400, - ); - } - - return $data; + return $request->getPayload()->all(); } - catch (\JsonException $exception) + catch (JsonException $exception) { + $this->getLogger()->error("Parsing JSON payload failed: {message}", [ + "message" => $exception->getMessage(), + "exception" => $exception, + "json" => (string) $request->getContent(), + ]); + throw new InvalidJsonRequestException( \sprintf("Parsing JSON payload failed: %s", $exception->getMessage()), 400,