Skip to content

Commit

Permalink
fix: revert add flag to force object (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Apr 21, 2022
1 parent e67638d commit c297139
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/JWT.php
Expand Up @@ -111,6 +111,10 @@ public static function decode(
if (null === ($payload = static::jsonDecode($payloadRaw))) {
throw new UnexpectedValueException('Invalid claims encoding');
}
if (is_array($payload)) {
// prevent PHP Fatal Error in edge-cases when payload is empty array
$payload = (object) $payload;
}
if (!$payload instanceof stdClass) {
throw new UnexpectedValueException('Payload must be a JSON object');
}
Expand Down Expand Up @@ -355,7 +359,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_FORCE_OBJECT);
$json = \json_encode($input, \JSON_UNESCAPED_SLASHES);
} else {
// PHP 5.3 only
$json = \json_encode($input);
Expand Down
9 changes: 9 additions & 0 deletions tests/JWTTest.php
Expand Up @@ -330,6 +330,15 @@ public function testDecodesEmptyArrayAsObject()
$this->assertEquals((object) $payload, $decoded);
}

public function testDecodesArraysInJWTAsArray()
{
$key = 'yma6Hq4XQegCVND8ef23OYgxSrC3IKqk';
$payload = ['foo' => [1,2,3]];
$jwt = JWT::encode($payload, $key, 'HS256');
$decoded = JWT::decode($jwt, new Key($key, 'HS256'));
$this->assertEquals($payload['foo'], $decoded->foo);
}

/**
* @runInSeparateProcess
* @dataProvider provideEncodeDecode
Expand Down

0 comments on commit c297139

Please sign in to comment.