Skip to content

Commit 526cd59

Browse files
EricRabiloshadmi
andauthored
Return no updates when performing an NX set to an existing array element (RedisJSON#593)
* Return no updates if the desired index is already present (nothing to add) * Create test for noError when NX SETting an existing array index * Update tests/pytest/test.py Co-authored-by: Omer Shadmi <76992134+oshadmi@users.noreply.github.com> Co-authored-by: Omer Shadmi <76992134+oshadmi@users.noreply.github.com>
1 parent 13a668a commit 526cd59

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/commands.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,18 @@ impl<'a, V: SelectValue> KeyValue<'a, V> {
283283
.collect())
284284
}
285285
} else if let StaticPathElement::ArrayIndex(_) = last {
286-
// if we reach here with array path we must be out of range
287-
// otherwise the path would be valid to be set and we would not
288-
// have reached here!!
289-
Err("ERR array index out of range".into())
286+
// if we reach here with array path we are either out of range
287+
// or no-oping an NX where the value is already present
288+
let mut selector = Selector::default();
289+
let res = selector
290+
.str_path(path)?
291+
.value(self.val)
292+
.select_with_paths(|_| true)?;
293+
if !res.is_empty() {
294+
Ok(Vec::new())
295+
} else {
296+
Err("ERR array index out of range".into())
297+
}
290298
} else {
291299
Err("ERR path not an object or array".into())
292300
}

tests/pytest/test.py

+7
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,13 @@ def testMultiPathResults(env):
955955
# make sure legacy json path returns single result
956956
env.expect("JSON.GET", "k", '.*[0,2]').equal('1')
957957

958+
def testIssue_597(env):
959+
env.expect("JSON.SET", "test", ".", "[0]").ok()
960+
env.assertEqual(env.execute_command("JSON.SET", "test", ".[0]", "[0]", "NX"), None)
961+
env.expect("JSON.SET", "test", ".[1]", "[0]", "NX").raiseError()
962+
# make sure value was not changed
963+
env.expect("JSON.GET", "test", ".").equal('[0]')
964+
958965
# class CacheTestCase(BaseReJSONTest):
959966
# @property
960967
# def module_args(env):

0 commit comments

Comments
 (0)