Skip to content

EspoCRM – Open Source CRM Application

Fix json\retrieve returning first array item for non-numeric path key

theluckystrike0f36367800fa
diff --git a/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php b/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php
index 60761ff627..0d148b7619 100644
--- a/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php
+++ b/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php
@@ -104,9 +104,11 @@ class RetrieveType extends BaseFunction
$key = array_shift($path);
if (is_array($item)) {
- $key = intval($key);
+ if (!ctype_digit($key)) {
+ return null;
+ }
- $subItem = $item[$key] ?? null;
+ $subItem = $item[(int) $key] ?? null;
if (is_null($subItem)) {
return null;
diff --git a/tests/unit/Espo/Core/Formula/EvaluatorTest.php b/tests/unit/Espo/Core/Formula/EvaluatorTest.php
index 9da492ad6c..c0606d710b 100644
--- a/tests/unit/Espo/Core/Formula/EvaluatorTest.php
+++ b/tests/unit/Espo/Core/Formula/EvaluatorTest.php
@@ -758,6 +758,31 @@ class EvaluatorTest extends TestCase
$this->assertEquals($value, $result);
}
+ public function testJsonRetrieveNonNumericKeyOnArray(): void
+ {
+ $value = (object) [
+ 'items' => [
+ (object) ['id' => 'test'],
+ ],
+ ];
+
+ $expression = "json\\retrieve(\$value, 'items.id')";
+
+ $result = $this->evaluator->process($expression, null, (object) [
+ 'value' => json_encode($value),
+ ]);
+
+ $this->assertNull($result);
+
+ $expression = "json\\retrieve(\$value, 'items.1a')";
+
+ $result = $this->evaluator->process($expression, null, (object) [
+ 'value' => json_encode($value),
+ ]);
+
+ $this->assertNull($result);
+ }
+
public function testNegate1()
{
$expression = "!string\contains('hello', 'test')";