Skip to content

Commit e1e7cd1

Browse files
authored
fix: Request::json() json errors when decoding empty string (#52389)
1 parent db768e4 commit e1e7cd1

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
@@ -404,7 +404,7 @@ public function get(string $key, mixed $default = null): mixed
404404
public function json($key = null, $default = null)
405405
{
406406
if (! isset($this->json)) {
407-
$this->json = new InputBag((array) json_decode($this->getContent(), true));
407+
$this->json = new InputBag((array) json_decode($this->getContent() ?: '[]', true));
408408
}
409409

410410
if (is_null($key)) {

tests/Http/HttpRequestTest.php

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

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

0 commit comments

Comments
 (0)