Skip to content

Commit ee1166c

Browse files
authored
fix: Request::json() json errors when decoding empty string (#52204)
1 parent 1301d9c commit ee1166c

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Illuminate/Http/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public function get(string $key, mixed $default = null): mixed
405405
public function json($key = null, $default = null)
406406
{
407407
if (! isset($this->json)) {
408-
$this->json = new InputBag((array) json_decode($this->getContent(), true));
408+
$this->json = new InputBag((array) json_decode($this->getContent() ?: '[]', true));
409409
}
410410

411411
if (is_null($key)) {

tests/Http/HttpRequestTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,4 +1606,14 @@ public function testItCanHaveObjectsInJsonPayload()
16061606

16071607
$this->assertSame(['name' => 'Laravel'], $request->get('framework'));
16081608
}
1609+
1610+
public function testItDoesNotGenerateJsonErrorsForEmptyContent()
1611+
{
1612+
// clear any existing errors
1613+
json_encode(null);
1614+
1615+
Request::create('', 'GET')->json();
1616+
1617+
$this->assertTrue(json_last_error() === JSON_ERROR_NONE);
1618+
}
16091619
}

0 commit comments

Comments
 (0)