Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

adds JSON_ERROR_NON_BACKED_ENUM to allowed JSON::encode() errors. #1707

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Util/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static function encode($data, int $options = 0, int $maxDepth = 512): str
$encodedData = json_encode($data, $options, $maxDepth);

$allowedErrors = [\JSON_ERROR_NONE, \JSON_ERROR_RECURSION, \JSON_ERROR_INF_OR_NAN, \JSON_ERROR_UNSUPPORTED_TYPE];
if (\defined('JSON_ERROR_NON_BACKED_ENUM')) {
$allowedErrors[] = \JSON_ERROR_NON_BACKED_ENUM;
}

$encounteredAnyError = json_last_error() !== \JSON_ERROR_NONE;

Expand Down
9 changes: 9 additions & 0 deletions tests/Util/Fixtures/NonBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Sentry\Tests\Util\Fixtures;
cleptric marked this conversation as resolved.
Show resolved Hide resolved

enum NonBackedEnum
{
case None;
case Some;
}
12 changes: 12 additions & 0 deletions tests/Util/JSONTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\TestCase;
use Sentry\Exception\JsonException;
use Sentry\Tests\Util\Fixtures\JsonSerializableClass;
use Sentry\Tests\Util\Fixtures\NonBackedEnum;
use Sentry\Tests\Util\Fixtures\SimpleClass;
use Sentry\Util\JSON;

Expand Down Expand Up @@ -143,6 +144,17 @@ public function testEncodeRespectsOptionsArgument(): void
$this->assertSame('{}', JSON::encode([], \JSON_FORCE_OBJECT));
}

/**
* The `JSON_ERROR_NON_BACKED_ENUM` constant is only exposed from 8.1.5 and up.
*
* @requires PHP >= 8.1.5
*/
public function testEncodeGracefullyHandlesUnitEnums(): void
{
$result = JSON::encode([NonBackedEnum::None, NonBackedEnum::Some]);
$this->assertSame('[0,0]', $result);
}

/**
* @dataProvider decodeDataProvider
*/
Expand Down
Loading