Skip to content

Commit 7e7ed83

Browse files
authored
Return empty array on none-existing path (RedisJSON#553)
1 parent c3ed883 commit 7e7ed83

File tree

2 files changed

+32
-42
lines changed

2 files changed

+32
-42
lines changed

src/commands.rs

+15-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::error::Error;
22
use crate::formatter::RedisJsonFormatter;
3+
use crate::manager::err_msg_json_path_doesnt_exist_with_param;
34
use crate::manager::{err_msg_json_expected, err_msg_json_path_doesnt_exist_with_param_or};
4-
use crate::manager::{err_msg_json_path_doesnt_exist, err_msg_json_path_doesnt_exist_with_param};
55
use crate::manager::{AddUpdateInfo, Manager, ReadHolder, SetUpdateInfo, UpdateInfo, WriteHolder};
66
use crate::nodevisitor::{StaticPathElement, StaticPathParser, VisitStatus};
77
use crate::redisjson::{normalize_arr_indices, Format, Path};
@@ -97,18 +97,12 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
9797
let v = self.get_first(path.get_path())?;
9898
Ok(self.resp_serialize_inner(v))
9999
} else {
100-
let res = self.get_values(path.get_path())?;
101-
if !res.is_empty() {
102-
Ok(res
103-
.iter()
104-
.map(|v| self.resp_serialize_inner(v))
105-
.collect::<Vec<RedisValue>>()
106-
.into())
107-
} else {
108-
Err(RedisError::String(
109-
err_msg_json_path_doesnt_exist_with_param(path.get_original()),
110-
))
111-
}
100+
Ok(self
101+
.get_values(path.get_path())?
102+
.iter()
103+
.map(|v| self.resp_serialize_inner(v))
104+
.collect::<Vec<RedisValue>>()
105+
.into())
112106
}
113107
}
114108

@@ -469,11 +463,7 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
469463
.into()
470464
})
471465
.collect::<Vec<RedisValue>>();
472-
if res.is_empty() {
473-
Err(Error::from(err_msg_json_path_doesnt_exist()))
474-
} else {
475-
Ok(res.into())
476-
}
466+
Ok(res.into())
477467
}
478468

479469
pub fn arr_index_legacy(
@@ -729,9 +719,7 @@ where
729719
let res = get_all_values_and_paths(path, doc)?;
730720
match res.is_empty() {
731721
false => Ok(filter_paths(res, f)),
732-
_ => Err(RedisError::String(
733-
err_msg_json_path_doesnt_exist_with_param(path),
734-
)),
722+
_ => Ok(vec![]),
735723
}
736724
}
737725

@@ -746,9 +734,7 @@ where
746734
let res = get_all_values_and_paths(path, doc)?;
747735
match res.is_empty() {
748736
false => Ok(filter_values(res, f)),
749-
_ => Err(RedisError::String(
750-
err_msg_json_path_doesnt_exist_with_param(path),
751-
)),
737+
_ => Ok(vec![]),
752738
}
753739
}
754740

@@ -1548,7 +1534,11 @@ pub fn command_json_arr_len<M: Manager>(
15481534
let values = find_all_values(path.get_path(), root, |v| {
15491535
v.get_type() == SelectValueType::Array
15501536
})?;
1551-
1537+
if is_legacy && values.is_empty() {
1538+
return Err(RedisError::String(
1539+
err_msg_json_path_doesnt_exist_with_param(path.get_original()),
1540+
));
1541+
}
15521542
let mut res = vec![];
15531543
for v in values {
15541544
let cur_val: RedisValue = match v {

tests/pytest/test_multi.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ def testObjKeysCommand(env):
641641
r.assertEqual(res, None)
642642

643643
# Test missing key
644-
r.expect('JSON.OBJKEYS', 'doc1', '$.nowhere').raiseError()
644+
r.assertEqual(r.execute_command('JSON.OBJKEYS', 'doc1', '$.nowhere'), [])
645645

646646
# Test default path
647647
res = r.execute_command('JSON.OBJKEYS', 'doc1')
@@ -664,7 +664,7 @@ def testObjLenCommand(env):
664664
r.expect('JSON.OBJLEN', 'non_existing_doc', '$..a').raiseError().contains("does not exist")
665665

666666
# Test missing path
667-
r.expect('JSON.OBJLEN', 'doc1', '$.nowhere').raiseError().contains("does not exist")
667+
r.assertEqual(r.execute_command('JSON.OBJLEN', 'doc1', '$.nowhere'), [])
668668

669669

670670
# Test legacy
@@ -858,7 +858,7 @@ def testRespCommand(env):
858858
r.assertEqual(resSingle, [['{', 'A1_B1', 10, 'A1_B2', 'false', 'A1_B3', ['{', 'A1_B3_C1', None, 'A1_B3_C2', ['[', 'A1_B3_C2_D1_1', 'A1_B3_C2_D1_2', '-19.5', 'A1_B3_C2_D1_4', 'A1_B3_C2_D1_5', ['{', 'A1_B3_C2_D1_6_E1', 'true']], 'A1_B3_C3', ['[', 1]], 'A1_B4', ['{', 'A1_B4_C1', 'foo']]])
859859

860860
# Test missing path
861-
r.expect('JSON.RESP', 'doc1', '$.nowhere').raiseError()
861+
r.assertEqual(r.execute_command('JSON.RESP', 'doc1', '$.nowhere'), [])
862862

863863
# Test missing key
864864
res = r.execute_command('JSON.RESP', 'non_existing_doc', '$..a')
@@ -984,7 +984,7 @@ def testErrorMessage(env):
984984

985985
# ARRAPPEND
986986
r.assertEqual(r.execute_command('JSON.ARRAPPEND', 'doc1', '$.string', '"abc"'), [None])
987-
r.expect('JSON.ARRAPPEND', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
987+
r.assertEqual(r.execute_command('JSON.ARRAPPEND', 'doc1', '$.nowhere', '"abc"'), [])
988988
r.expect('JSON.ARRAPPEND', 'doc_none', '$.string', '"abc"').raiseError().contains("doesn't exist")
989989
r.expect('JSON.ARRAPPEND', 'hash_key', '$.string', '"abc"').raiseError().contains("wrong Redis type")
990990

@@ -1004,7 +1004,7 @@ def testErrorMessage(env):
10041004

10051005
# ARRPOP
10061006
r.assertEqual(r.execute_command('JSON.ARRPOP', 'doc1', '$.string', '"abc"'), [None])
1007-
r.expect('JSON.ARRPOP', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
1007+
r.assertEqual(r.execute_command('JSON.ARRPOP', 'doc1', '$.nowhere', '"abc"'), [])
10081008
r.expect('JSON.ARRPOP', 'doc_none', '$..string', '"abc"').raiseError().contains("doesn't exist")
10091009
r.expect('JSON.ARRPOP', 'hash_key', '$..string', '"abc"').raiseError().contains("wrong Redis type")
10101010

@@ -1024,7 +1024,7 @@ def testErrorMessage(env):
10241024

10251025
# ARRINDEX
10261026
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'doc1', '$.number', '"abc"'), [None])
1027-
r.expect('JSON.ARRINDEX', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
1027+
r.assertEqual(r.execute_command('JSON.ARRINDEX', 'doc1', '$.nowhere', '"abc"'), [])
10281028
r.expect('JSON.ARRINDEX', 'doc_none', '$.number', '"abc"').raiseError().contains("does not exist")
10291029
r.expect('JSON.ARRINDEX', 'hash_key', '$.number', '"abc"').raiseError().contains("wrong Redis type")
10301030

@@ -1044,7 +1044,7 @@ def testErrorMessage(env):
10441044

10451045
# ARRINSERT
10461046
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'doc1', '$.string', 0, '"abc"'), [None])
1047-
r.expect('JSON.ARRINSERT', 'doc1', '$.nowhere', 0, '"abc"').raiseError().contains("does not exist")
1047+
r.assertEqual(r.execute_command('JSON.ARRINSERT', 'doc1', '$.nowhere', 0, '"abc"'), [])
10481048
r.expect('JSON.ARRINSERT', 'doc_none', '$.string', 0, '"abc"').raiseError().contains("doesn't exist")
10491049
r.expect('JSON.ARRINSERT', 'hash_key', '$.string', 0, '"abc"').raiseError().contains("wrong Redis type")
10501050

@@ -1064,7 +1064,7 @@ def testErrorMessage(env):
10641064

10651065
# ARRLEN
10661066
r.assertEqual(r.execute_command('JSON.ARRLEN', 'doc1', '$.string', '"abc"'), [None])
1067-
r.expect('JSON.ARRLEN', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
1067+
r.assertEqual(r.execute_command('JSON.ARRLEN', 'doc1', '$.nowhere', '"abc"'), [])
10681068
r.expect('JSON.ARRLEN', 'doc_none', '$.string', '"abc"').raiseError().contains("doesn't exist")
10691069
r.expect('JSON.ARRLEN', 'hash_key', '$.string', '"abc"').raiseError().contains("wrong Redis type")
10701070

@@ -1084,7 +1084,7 @@ def testErrorMessage(env):
10841084

10851085
# ARRTRIM
10861086
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'doc1', '$.string', 0, 1), [None])
1087-
r.expect('JSON.ARRTRIM', 'doc1', '$.nowhere', 0, 1).raiseError().contains("does not exist")
1087+
r.assertEqual(r.execute_command('JSON.ARRTRIM', 'doc1', '$.nowhere', 0, 1), [])
10881088
r.expect('JSON.ARRTRIM', 'doc_none', '$.string', 0, 1).raiseError().contains("doesn't exist")
10891089
r.expect('JSON.ARRTRIM', 'hash_key', '$.string', 0, 1).raiseError().contains("wrong Redis type")
10901090

@@ -1104,7 +1104,7 @@ def testErrorMessage(env):
11041104

11051105
# OBJKEYS
11061106
r.assertEqual(r.execute_command('JSON.OBJKEYS', 'doc1', '$.string'), [None])
1107-
r.expect('JSON.OBJKEYS', 'doc1', '$.nowhere').raiseError().contains("does not exist")
1107+
r.assertEqual(r.execute_command('JSON.OBJKEYS', 'doc1', '$.nowhere'), [])
11081108
r.expect('JSON.OBJKEYS', 'doc_none', '$.string').raiseError().contains("doesn't exist")
11091109
r.expect('JSON.OBJKEYS', 'hash_key', '$.string').raiseError().contains("wrong Redis type")
11101110

@@ -1124,7 +1124,7 @@ def testErrorMessage(env):
11241124

11251125
# OBJLEN
11261126
r.assertEqual(r.execute_command('JSON.OBJLEN', 'doc1', '$.string'), [None])
1127-
r.expect('JSON.OBJLEN', 'doc1', '$.nowhere').raiseError().contains("does not exist")
1127+
r.assertEqual(r.execute_command('JSON.OBJLEN', 'doc1', '$.nowhere'), [])
11281128
r.expect('JSON.OBJLEN', 'doc_none', '$.string').raiseError().contains("does not exist")
11291129
r.expect('JSON.OBJLEN', 'hash_key', '.string').raiseError().contains("wrong Redis type")
11301130

@@ -1144,7 +1144,7 @@ def testErrorMessage(env):
11441144

11451145
# NUMINCRBY
11461146
r.assertEqual(r.execute_command('JSON.NUMINCRBY', 'doc1', '$.string', 3), '[null]')
1147-
r.expect('JSON.NUMINCRBY', 'doc1', '$.nowhere', 3).raiseError().contains("does not exist")
1147+
r.assertEqual(r.execute_command('JSON.NUMINCRBY', 'doc1', '$.nowhere', 3), '[]')
11481148
r.expect('JSON.NUMINCRBY', 'doc_none', '$.string', 3).raiseError().contains("doesn't exist")
11491149
r.expect('JSON.NUMINCRBY', 'hash_key', '$.string', 3).raiseError().contains("wrong Redis type")
11501150

@@ -1165,7 +1165,7 @@ def testErrorMessage(env):
11651165

11661166
# NUMMULTBY
11671167
r.assertEqual(r.execute_command('JSON.NUMMULTBY', 'doc1', '$.string', 3), '[null]')
1168-
r.expect('JSON.NUMMULTBY', 'doc1', '$.nowhere', 3).raiseError().contains("does not exist")
1168+
r.assertEqual(r.execute_command('JSON.NUMMULTBY', 'doc1', '$.nowhere', 3), '[]')
11691169
r.expect('JSON.NUMMULTBY', 'doc_none', '$.string', 3).raiseError().contains("doesn't exist")
11701170
r.expect('JSON.NUMMULTBY', 'hash_key', '$.string', 3).raiseError().contains("wrong Redis type")
11711171

@@ -1185,7 +1185,7 @@ def testErrorMessage(env):
11851185

11861186
# STRAPPEND
11871187
r.assertEqual(r.execute_command('JSON.STRAPPEND', 'doc1', '$.number', '"abc"'), [None])
1188-
r.expect('JSON.STRAPPEND', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
1188+
r.assertEqual(r.execute_command('JSON.STRAPPEND', 'doc1', '$.nowhere', '"abc"'), [])
11891189
r.expect('JSON.STRAPPEND', 'doc_none', '$.number', '"abc"').raiseError().contains("doesn't exist")
11901190
r.expect('JSON.STRAPPEND', 'hash_key', '$.number', '"abc"').raiseError().contains("wrong Redis type")
11911191

@@ -1206,7 +1206,7 @@ def testErrorMessage(env):
12061206

12071207
# STRLEN
12081208
r.assertEqual(r.execute_command('JSON.STRLEN', 'doc1', '$.object', '"abc"'), [None])
1209-
r.expect('JSON.STRLEN', 'doc1', '$.nowhere', '"abc"').raiseError().contains("does not exist")
1209+
r.assertEqual(r.execute_command('JSON.STRLEN', 'doc1', '$.nowhere', '"abc"'), [])
12101210
r.expect('JSON.STRLEN', 'doc_none', '$.object', '"abc"').raiseError().contains("doesn't exist")
12111211
r.expect('JSON.STRLEN', 'hash_key', '$.object', '"abc"').raiseError().contains("wrong Redis type")
12121212

@@ -1226,7 +1226,7 @@ def testErrorMessage(env):
12261226

12271227
# TOGGLE
12281228
r.assertEqual(r.execute_command('JSON.TOGGLE', 'doc1', '$.object'), [None])
1229-
r.expect('JSON.TOGGLE', 'doc1', '$.nowhere').raiseError().contains("does not exist")
1229+
r.assertEqual(r.execute_command('JSON.TOGGLE', 'doc1', '$.nowhere'), [])
12301230
r.expect('JSON.TOGGLE', 'doc_none', '$.object').raiseError().contains("doesn't exist")
12311231
r.expect('JSON.TOGGLE', 'hash_key', '$.object').raiseError().contains("wrong Redis type")
12321232

@@ -1273,7 +1273,7 @@ def testErrorMessage(env):
12731273
"""
12741274

12751275
# RESP
1276-
r.expect('JSON.RESP', 'doc1', '$.nowhere').raiseError().contains("does not exist")
1276+
r.assertEqual(r.execute_command('JSON.RESP', 'doc1', '$.nowhere'), [])
12771277
r.assertEqual(r.execute_command('JSON.RESP', 'doc_none', '$.object'), None)
12781278
r.assertEqual(r.execute_command('JSON.RESP', 'doc_none', '$.object'), None)
12791279
r.expect('JSON.RESP', 'hash_key', '$.object').raiseError().contains("wrong Redis type")

0 commit comments

Comments
 (0)