Skip to content

Commit c8023b3

Browse files
committed
Fix for #1171 utf-16 issue in smartserializer
1 parent 527b2f1 commit c8023b3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/Elasticsearch/Serializers/SmartSerializer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use Elasticsearch\Common\Exceptions;
2222
use Elasticsearch\Common\Exceptions\Serializer\JsonErrorException;
23+
use JsonException;
2324

2425
if (!defined('JSON_INVALID_UTF8_SUBSTITUTE')) {
2526
//PHP < 7.2 Define it as 0 so it does nothing
@@ -84,9 +85,12 @@ private function decode(?string $data): array
8485
try {
8586
$result = json_decode($data, true, 512, JSON_THROW_ON_ERROR);
8687
return $result;
87-
} catch (\JsonException $e) {
88-
$result = $result ?? [];
89-
throw new JsonErrorException($e->getCode(), $data, $result);
88+
} catch (JsonException $e) {
89+
switch ($e->getCode()) {
90+
case JSON_ERROR_UTF16:
91+
return $this->decode(str_replace('\\', '\\\\', $data));
92+
}
93+
throw new JsonErrorException($e->getCode(), $data, $result ?? []);
9094
}
9195
}
9296

tests/Elasticsearch/Tests/Serializers/SmartSerializerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,18 @@ public function testThrowJsonErrorException()
4545

4646
$result = $this->serializer->deserialize('{ "foo" : bar" }', []);
4747
}
48+
49+
/**
50+
* Single unpaired UTF-16 surrogate in unicode escape
51+
*
52+
* @requires PHP 7.3
53+
* @see https://github.com/elastic/elasticsearch-php/issues/1171
54+
*/
55+
public function testSingleUnpairedUTF16SurrogateInUnicodeEscape()
56+
{
57+
$json = '{ "data": "ud83d\ude4f" }';
58+
59+
$result = $this->serializer->deserialize($json, []);
60+
$this->assertEquals($result['data'], 'ud83d\ude4f');
61+
}
4862
}

0 commit comments

Comments
 (0)