Skip to content

Commit

Permalink
fix: Request::json() json errors when decoding empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
calebdw committed Jul 21, 2024
1 parent 9d7c869 commit 068c10a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,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)) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Http/HttpRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1606,4 +1606,14 @@ public function testItCanHaveObjectsInJsonPayload()

$this->assertSame(['name' => 'Laravel'], $request->get('framework'));
}

public function testItDoesNotGenerateJsonErrors()
{
// clear any existing errors
json_encode(null);

Request::create('', 'GET')->json();

$this->assertTrue(json_last_error() === JSON_ERROR_NONE);
}
}

0 comments on commit 068c10a

Please sign in to comment.