This is an automated email from the ASF dual-hosted git repository. twice pushed a commit to branch unstable in repository https://gitbox.apache.org/repos/asf/kvrocks.git
The following commit(s) were added to refs/heads/unstable by this push: new 5e044557 Fix JSON.[STRLEN|OBJLEN] response inconsistently with Redis when the path doesn't exist (#2336) 5e044557 is described below commit 5e0445573313ea564eaf19ba0ea5176e9caf9ec4 Author: lizhenglei <127465317+jackyyyyys...@users.noreply.github.com> AuthorDate: Thu May 30 19:18:56 2024 +0800 Fix JSON.[STRLEN|OBJLEN] response inconsistently with Redis when the path doesn't exist (#2336) Co-authored-by: 80597928 <673421...@qq.com> Co-authored-by: hulk <hulk.webs...@gmail.com> Co-authored-by: Twice <twice.m...@gmail.com> --- src/commands/cmd_json.cc | 16 ++++++++++++---- tests/gocase/unit/type/json/json_test.go | 8 ++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/commands/cmd_json.cc b/src/commands/cmd_json.cc index 43421399..887cd6c6 100644 --- a/src/commands/cmd_json.cc +++ b/src/commands/cmd_json.cc @@ -383,8 +383,12 @@ class CommandJsonObjLen : public Commander { Optionals<uint64_t> results; auto s = json.ObjLen(args_[1], path, &results); if (s.IsNotFound()) { - *output = conn->NilString(); - return Status::OK(); + if (args_.size() == 2) { + *output = conn->NilString(); + return Status::OK(); + } else { + return {Status::RedisExecErr, "Path '" + args_[2] + "' does not exist or not an object"}; + } } if (!s.ok()) return {Status::RedisExecErr, s.ToString()}; @@ -560,8 +564,12 @@ class CommandJsonStrLen : public Commander { Optionals<uint64_t> results; auto s = json.StrLen(args_[1], path, &results); if (s.IsNotFound()) { - *output = conn->NilString(); - return Status::OK(); + if (args_.size() == 2) { + *output = conn->NilString(); + return Status::OK(); + } else { + return {Status::RedisExecErr, "could not perform this operation on a key that doesn't exist"}; + } } if (!s.ok()) return {Status::RedisExecErr, s.ToString()}; diff --git a/tests/gocase/unit/type/json/json_test.go b/tests/gocase/unit/type/json/json_test.go index e30635d4..0f2bbf8f 100644 --- a/tests/gocase/unit/type/json/json_test.go +++ b/tests/gocase/unit/type/json/json_test.go @@ -180,7 +180,9 @@ func TestJson(t *testing.T) { result2 = append(result2, int64(3), int64(5), interface{}(nil)) require.NoError(t, rdb.Do(ctx, "JSON.SET", "a", "$", `{"a":"foo", "nested": {"a": "hello"}, "nested2": {"a": 31}}`).Err()) require.Equal(t, rdb.Do(ctx, "JSON.STRLEN", "a", "$..a").Val(), result2) - require.ErrorIs(t, rdb.Do(ctx, "JSON.STRLEN", "not_exists", "$").Err(), redis.Nil) + require.Error(t, rdb.Do(ctx, "JSON.STRLEN", "not_exists", "$").Err()) + require.ErrorIs(t, rdb.Do(ctx, "JSON.STRLEN", "not_exists").Err(), redis.Nil) + }) t.Run("Merge basics", func(t *testing.T) { @@ -579,7 +581,9 @@ func TestJson(t *testing.T) { require.NoError(t, err) require.EqualValues(t, 0, len(vals)) - err = rdb.Do(ctx, "JSON.OBJLEN", "no-such-json-key", "$").Err() + require.Error(t, rdb.Do(ctx, "JSON.OBJLEN", "no-such-json-key", "$").Err()) + err = rdb.Do(ctx, "JSON.OBJLEN", "no-such-json-key").Err() + require.EqualError(t, err, redis.Nil.Error()) })