From 7d9541de5d35b620a927dddd7de6699348280fc7 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 21 Jun 2023 17:59:30 +0200 Subject: [PATCH] [HttpFoundation] Make Request::getPayload() return an empty InputBag if request body is empty --- .../Component/HttpFoundation/Request.php | 20 ++++++++++++++++++- .../HttpFoundation/Tests/RequestTest.php | 3 +++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index 9445a4b28127b..0bef6f8d70796 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -1513,7 +1513,25 @@ public function getContent(bool $asResource = false) */ public function getPayload(): InputBag { - return $this->request->count() ? clone $this->request : new InputBag($this->toArray()); + if ($this->request->count()) { + return clone $this->request; + } + + if ('' === $content = $this->getContent()) { + return new InputBag([]); + } + + try { + $content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR); + } catch (\JsonException $e) { + throw new JsonException('Could not decode request body.', $e->getCode(), $e); + } + + if (!\is_array($content)) { + throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content))); + } + + return new InputBag($content); } /** diff --git a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php index 5e8df28bb5713..308e9e6fd8863 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/RequestTest.php @@ -1309,6 +1309,9 @@ public function testGetPayload() $req = new Request([], ['foo' => 'bar'], [], [], [], [], json_encode(['baz' => 'qux'])); $this->assertSame(['foo' => 'bar'], $req->getPayload()->all()); + + $req = new Request([], [], [], [], [], [], ''); + $this->assertSame([], $req->getPayload()->all()); } /** pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy