Skip to content

Commit c297139

Browse files
authored
fix: revert add flag to force object (#420)
1 parent e67638d commit c297139

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/JWT.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ public static function decode(
111111
if (null === ($payload = static::jsonDecode($payloadRaw))) {
112112
throw new UnexpectedValueException('Invalid claims encoding');
113113
}
114+
if (is_array($payload)) {
115+
// prevent PHP Fatal Error in edge-cases when payload is empty array
116+
$payload = (object) $payload;
117+
}
114118
if (!$payload instanceof stdClass) {
115119
throw new UnexpectedValueException('Payload must be a JSON object');
116120
}
@@ -355,7 +359,7 @@ public static function jsonDecode(string $input)
355359
public static function jsonEncode(array $input): string
356360
{
357361
if (PHP_VERSION_ID >= 50400) {
358-
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES|\JSON_FORCE_OBJECT);
362+
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
359363
} else {
360364
// PHP 5.3 only
361365
$json = \json_encode($input);

tests/JWTTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,15 @@ public function testDecodesEmptyArrayAsObject()
330330
$this->assertEquals((object) $payload, $decoded);
331331
}
332332

333+
public function testDecodesArraysInJWTAsArray()
334+
{
335+
$key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk';
336+
$payload = ['foo' => [1,2,3]];
337+
$jwt = JWT::encode($payload, $key, 'HS256');
338+
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
339+
$this->assertEquals($payload['foo'], $decoded->foo);
340+
}
341+
333342
/**
334343
* @runInSeparateProcess
335344
* @dataProvider provideEncodeDecode

0 commit comments

Comments
 (0)