From 314cca9c937efcf13bf177a1da4e7877bcd51c83 Mon Sep 17 00:00:00 2001 From: ju1ius Date: Tue, 27 Feb 2024 17:02:36 +0100 Subject: [PATCH 1/5] adds `JSON_ERROR_NON_BACKED_ENUM` to allowed errors. The `JSON_ERROR_NON_BACKED_ENUM` error code is not yet documented (see https://github.com/php/doc-en/issues/2747), but it is returned when trying to encode encode a non-backed enum to JSON. Since this error code is semantically equivalent to `JSON_ERROR_UNSUPPORTED_TYPE` which is already in the allow list, it makes sense to also add it. --- src/Util/JSON.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Util/JSON.php b/src/Util/JSON.php index 9c568c52f..a33ba1b79 100644 --- a/src/Util/JSON.php +++ b/src/Util/JSON.php @@ -34,7 +34,7 @@ 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]; + $allowedErrors = [\JSON_ERROR_NONE, \JSON_ERROR_RECURSION, \JSON_ERROR_INF_OR_NAN, \JSON_ERROR_UNSUPPORTED_TYPE, \JSON_ERROR_NON_BACKED_ENUM]; $encounteredAnyError = json_last_error() !== \JSON_ERROR_NONE; From dd5da73d71a6decf7e2aa6e9d80007591d39681f Mon Sep 17 00:00:00 2001 From: ju1ius Date: Tue, 27 Feb 2024 17:30:42 +0100 Subject: [PATCH 2/5] adds compatibility check for PHP < 8.1 --- src/Util/JSON.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Util/JSON.php b/src/Util/JSON.php index a33ba1b79..4ade8a6cb 100644 --- a/src/Util/JSON.php +++ b/src/Util/JSON.php @@ -34,7 +34,10 @@ 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, \JSON_ERROR_NON_BACKED_ENUM]; + $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; From 829b009ab31ec56bf05d7df2ed9be2a60f228bf3 Mon Sep 17 00:00:00 2001 From: ju1ius Date: Tue, 27 Feb 2024 18:12:47 +0100 Subject: [PATCH 3/5] fixes code-style --- src/Util/JSON.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Util/JSON.php b/src/Util/JSON.php index 4ade8a6cb..9824cbd74 100644 --- a/src/Util/JSON.php +++ b/src/Util/JSON.php @@ -35,7 +35,7 @@ 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')) { + if (\defined('JSON_ERROR_NON_BACKED_ENUM')) { $allowedErrors[] = \JSON_ERROR_NON_BACKED_ENUM; } From a41b48bbfab5ede0ffd39d99f547e7213389807d Mon Sep 17 00:00:00 2001 From: ju1ius Date: Tue, 27 Feb 2024 19:38:04 +0100 Subject: [PATCH 4/5] adds test-case for #1707 --- tests/Util/Fixtures/NonBackedEnum.php | 9 +++++++++ tests/Util/JSONTest.php | 12 ++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/Util/Fixtures/NonBackedEnum.php diff --git a/tests/Util/Fixtures/NonBackedEnum.php b/tests/Util/Fixtures/NonBackedEnum.php new file mode 100644 index 000000000..c482ce438 --- /dev/null +++ b/tests/Util/Fixtures/NonBackedEnum.php @@ -0,0 +1,9 @@ +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 */ From c78a42c44260d5e3164b2ffc225c2b76631506de Mon Sep 17 00:00:00 2001 From: Michi Hoffmann Date: Tue, 27 Feb 2024 20:02:55 +0100 Subject: [PATCH 5/5] Update tests/Util/Fixtures/NonBackedEnum.php --- tests/Util/Fixtures/NonBackedEnum.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Util/Fixtures/NonBackedEnum.php b/tests/Util/Fixtures/NonBackedEnum.php index c482ce438..8cef91f05 100644 --- a/tests/Util/Fixtures/NonBackedEnum.php +++ b/tests/Util/Fixtures/NonBackedEnum.php @@ -1,4 +1,6 @@ -