From ccf720459b2cf35100203224912a5671d67701e2 Mon Sep 17 00:00:00 2001 From: divine <48183131+divine@users.noreply.github.com> Date: Mon, 2 May 2022 00:42:48 +0300 Subject: [PATCH 1/2] fix: json encoder invalid utf8 characters Co-Authored-By: AntonioCS <1086401+antoniocs@users.noreply.github.com> --- src/Serializer/JsonEncoder.php | 2 +- tests/Serializer/JsonEncoderTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Serializer/JsonEncoder.php b/src/Serializer/JsonEncoder.php index 2a95e510bb0..4ac574cc237 100644 --- a/src/Serializer/JsonEncoder.php +++ b/src/Serializer/JsonEncoder.php @@ -40,7 +40,7 @@ public function __construct(string $format, BaseJsonEncoder $jsonEncoder = null) } // Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML. - $jsonEncodeOptions = \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_UNICODE; + $jsonEncodeOptions = \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_UNICODE | \JSON_INVALID_UTF8_IGNORE; if (interface_exists(AdvancedNameConverterInterface::class)) { $jsonEncode = new JsonEncode(['json_encode_options' => $jsonEncodeOptions]); $jsonDecode = new JsonDecode(['json_decode_associative' => true]); diff --git a/tests/Serializer/JsonEncoderTest.php b/tests/Serializer/JsonEncoderTest.php index a4138b3fffd..8374288099e 100644 --- a/tests/Serializer/JsonEncoderTest.php +++ b/tests/Serializer/JsonEncoderTest.php @@ -54,4 +54,18 @@ public function testDecode() { $this->assertEquals(['foo' => 'bar'], $this->encoder->decode('{"foo":"bar"}', 'json')); } + + public function testUTF8EncodedString() + { + $data = ['foo' => 'Über']; + + $this->assertEquals('{"foo":"Über"}', $this->encoder->encode($data, 'json')); + } + + public function testUTF8MalformedHandlingEncoding() + { + $data = ['foo' => pack('H*', 'B11111')]; + + $this->assertEquals('{"foo":"\u0011\u0011"}', $this->encoder->encode($data, 'json')); + } } From f86655078a6b181094f328f811dab9303f5d6eab Mon Sep 17 00:00:00 2001 From: divine <48183131+divine@users.noreply.github.com> Date: Tue, 3 May 2022 03:12:01 +0300 Subject: [PATCH 2/2] fix: skip test for php 7.1 --- src/Serializer/JsonEncoder.php | 2 +- tests/Serializer/JsonEncoderTest.php | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Serializer/JsonEncoder.php b/src/Serializer/JsonEncoder.php index 4ac574cc237..53fa2f8aa1f 100644 --- a/src/Serializer/JsonEncoder.php +++ b/src/Serializer/JsonEncoder.php @@ -40,7 +40,7 @@ public function __construct(string $format, BaseJsonEncoder $jsonEncoder = null) } // Encode <, >, ', &, and " characters in the JSON, making it also safe to be embedded into HTML. - $jsonEncodeOptions = \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_UNICODE | \JSON_INVALID_UTF8_IGNORE; + $jsonEncodeOptions = \JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT | \JSON_UNESCAPED_UNICODE | (\PHP_VERSION_ID >= 70200 ? \JSON_INVALID_UTF8_IGNORE : 0); if (interface_exists(AdvancedNameConverterInterface::class)) { $jsonEncode = new JsonEncode(['json_encode_options' => $jsonEncodeOptions]); $jsonDecode = new JsonDecode(['json_decode_associative' => true]); diff --git a/tests/Serializer/JsonEncoderTest.php b/tests/Serializer/JsonEncoderTest.php index 8374288099e..2be8c917f96 100644 --- a/tests/Serializer/JsonEncoderTest.php +++ b/tests/Serializer/JsonEncoderTest.php @@ -64,8 +64,10 @@ public function testUTF8EncodedString() public function testUTF8MalformedHandlingEncoding() { - $data = ['foo' => pack('H*', 'B11111')]; + if (\PHP_VERSION_ID >= 70200) { + $data = ['foo' => pack('H*', 'B11111')]; - $this->assertEquals('{"foo":"\u0011\u0011"}', $this->encoder->encode($data, 'json')); + $this->assertEquals('{"foo":"\u0011\u0011"}', $this->encoder->encode($data, 'json')); + } } }