From e1e7cd18a3c3b64c1ac2eb8a1359e173884b081d Mon Sep 17 00:00:00 2001 From: Caleb White Date: Mon, 5 Aug 2024 12:20:47 -0500 Subject: [PATCH] fix: Request::json() json errors when decoding empty string (#52389) --- src/Illuminate/Http/Request.php | 2 +- tests/Http/HttpRequestTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Http/Request.php b/src/Illuminate/Http/Request.php index 1a36ec2d0f1e..d7c8745f719b 100644 --- a/src/Illuminate/Http/Request.php +++ b/src/Illuminate/Http/Request.php @@ -404,7 +404,7 @@ public function get(string $key, mixed $default = null): mixed public function json($key = null, $default = null) { if (! isset($this->json)) { - $this->json = new InputBag((array) json_decode($this->getContent(), true)); + $this->json = new InputBag((array) json_decode($this->getContent() ?: '[]', true)); } if (is_null($key)) { diff --git a/tests/Http/HttpRequestTest.php b/tests/Http/HttpRequestTest.php index 75a0cf5571af..b0d3f53e1242 100644 --- a/tests/Http/HttpRequestTest.php +++ b/tests/Http/HttpRequestTest.php @@ -1608,4 +1608,14 @@ public function testItCanHaveObjectsInJsonPayload() $this->assertSame(['name' => 'Laravel'], $request->get('framework')); } + + public function testItDoesNotGenerateJsonErrorsForEmptyContent() + { + // clear any existing errors + json_encode(null); + + Request::create('', 'GET')->json(); + + $this->assertTrue(json_last_error() === JSON_ERROR_NONE); + } }