From 118f0fbfceab88798db7e321cc185cb20296206c Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 4 Apr 2022 13:51:05 -0600 Subject: [PATCH 1/2] fix: add flag to force object --- src/JWT.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JWT.php b/src/JWT.php index 843d0ae0..d631c390 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -353,7 +353,7 @@ public static function jsonDecode(string $input) public static function jsonEncode(array $input): string { if (PHP_VERSION_ID >= 50400) { - $json = \json_encode($input, \JSON_UNESCAPED_SLASHES); + $json = \json_encode($input, \JSON_UNESCAPED_SLASHES|\JSON_FORCE_OBJECT); } else { // PHP 5.3 only $json = \json_encode($input); From 42e225b656798a1d6e4b7a63c545576c2e95941f Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 13 Apr 2022 16:31:50 -0500 Subject: [PATCH 2/2] add test --- tests/JWTTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 8b23ad6b..e1984b34 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -321,6 +321,15 @@ public function testRSEncodeDecodeWithPassphrase() $this->assertEquals($decoded, $expected); } + public function testDecodesEmptyArrayAsObject() + { + $key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk'; + $payload = []; + $jwt = JWT::encode($payload, $key, 'HS256'); + $decoded = JWT::decode($jwt, new Key($key, 'HS256')); + $this->assertEquals((object) $payload, $decoded); + } + /** * @runInSeparateProcess * @dataProvider provideEncodeDecode