From 8b96f2e4521cdc57f82288c493e5d53a688a3a40 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 25 Apr 2022 17:14:15 +0200 Subject: [PATCH 1/3] Fix json_last_error issue with setData --- src/Illuminate/Http/JsonResponse.php | 3 +++ tests/Integration/Http/JsonResponseTest.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Illuminate/Http/JsonResponse.php b/src/Illuminate/Http/JsonResponse.php index b2e205ce8fe0..c889e30b9194 100755 --- a/src/Illuminate/Http/JsonResponse.php +++ b/src/Illuminate/Http/JsonResponse.php @@ -74,6 +74,9 @@ public function setData($data = []) { $this->original = $data; + // Make sure json_last_error() is cleared... + json_decode('[]'); + if ($data instanceof Jsonable) { $this->data = $data->toJson($this->encodingOptions); } elseif ($data instanceof JsonSerializable) { diff --git a/tests/Integration/Http/JsonResponseTest.php b/tests/Integration/Http/JsonResponseTest.php index e27efdf0e992..4d24601def72 100644 --- a/tests/Integration/Http/JsonResponseTest.php +++ b/tests/Integration/Http/JsonResponseTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Integration\Http; +use Illuminate\Contracts\Support\Jsonable; use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Route; use Orchestra\Testbench\TestCase; @@ -27,4 +28,22 @@ public function jsonSerialize(): string $this->get('/response'); } + + public function testResponseSetDataPassesWithPriorJsonErrors() + { + $response = new JsonResponse(); + + // Trigger json_last_error() to have a non-zero value... + json_encode(['a' => acos(2)]); + + $response->setData(new class implements Jsonable + { + public function toJson($options = 0): string + { + return "{}"; + } + }); + + $this->assertJson($response->getContent()); + } } From dde407d215fb3b6d1225ef9bda70655123bec352 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 25 Apr 2022 15:15:04 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- tests/Integration/Http/JsonResponseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Http/JsonResponseTest.php b/tests/Integration/Http/JsonResponseTest.php index 4d24601def72..adb764c237af 100644 --- a/tests/Integration/Http/JsonResponseTest.php +++ b/tests/Integration/Http/JsonResponseTest.php @@ -40,7 +40,7 @@ public function testResponseSetDataPassesWithPriorJsonErrors() { public function toJson($options = 0): string { - return "{}"; + return '{}'; } }); From bf15d58095b0b994da5e5afc2fdc0337be4892b7 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 25 Apr 2022 13:16:27 -0500 Subject: [PATCH 3/3] Update JsonResponse.php --- src/Illuminate/Http/JsonResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Http/JsonResponse.php b/src/Illuminate/Http/JsonResponse.php index c889e30b9194..84a68f971491 100755 --- a/src/Illuminate/Http/JsonResponse.php +++ b/src/Illuminate/Http/JsonResponse.php @@ -74,7 +74,7 @@ public function setData($data = []) { $this->original = $data; - // Make sure json_last_error() is cleared... + // Ensure json_last_error() is cleared... json_decode('[]'); if ($data instanceof Jsonable) {