From 33e7f4e3efbc1bfe53d55ded333770e2f0c95852 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 23 Oct 2023 20:59:59 +0300 Subject: [PATCH] json retrive support empty path --- .../Functions/JsonGroup/RetrieveType.php | 29 +++++++++++-------- .../unit/Espo/Core/Formula/EvaluatorTest.php | 15 ++++++++++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php b/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php index 5f9fdd6e04..d2d43c84c2 100644 --- a/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php +++ b/application/Espo/Core/Formula/Functions/JsonGroup/RetrieveType.php @@ -29,26 +29,31 @@ namespace Espo\Core\Formula\Functions\JsonGroup; -use Espo\Core\Formula\{ - Functions\BaseFunction, - ArgumentList, -}; +use Espo\Core\Formula\ArgumentList; +use Espo\Core\Formula\Exceptions\Error; +use Espo\Core\Formula\Exceptions\ExecutionException; +use Espo\Core\Formula\Exceptions\TooFewArguments; +use Espo\Core\Formula\Functions\BaseFunction; class RetrieveType extends BaseFunction { /** * @return mixed - * @throws \Espo\Core\Formula\Exceptions\TooFewArguments - * @throws \Espo\Core\Formula\Exceptions\Error + * @throws TooFewArguments + * @throws Error + * @throws ExecutionException */ public function process(ArgumentList $args) { - if (count($args) < 2) { + if (count($args) < 1) { $this->throwTooFewArguments(); } $jsonString = $this->evaluate($args[0]); - $path = $this->evaluate($args[1]); + + $path = count($args) > 1 ? + $this->evaluate($args[1]) : + ''; if (!is_string($jsonString)) { $this->throwBadArgumentType(1, 'string'); @@ -58,10 +63,6 @@ class RetrieveType extends BaseFunction $this->throwBadArgumentType(2, 'string'); } - if ($path === '') { - $this->throwBadArgumentValue(2); - } - $item = json_decode($jsonString); $pathArray = $this->splitPath($path); @@ -75,6 +76,10 @@ class RetrieveType extends BaseFunction */ private function splitPath(string $path): array { + if ($path === '') { + return []; + } + /** @var string[] $pathArray */ $pathArray = preg_split('/(?assertEquals('test', $result); } + public function testJsonRetrieve8() + { + $value = (object) [ + 0 => 'test', + ]; + + $expression = "json\\retrieve(\$value)"; + + $result = $this->evaluator->process($expression, null, (object) [ + 'value' => json_encode($value), + ]); + + $this->assertEquals($value, $result); + } + public function testNegate1() { $expression = "!string\contains('hello', 'test')";