Skip to content

Commit e2fb4ff

Browse files
authored
fix: JSON.DEL to return 0 for non-existing keys instead of error (#5343)
Fixed: #5331
1 parent 97a15dc commit e2fb4ff

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/server/json_family.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,12 @@ OpResult<long> OpDel(const OpArgs& op_args, string_view key, string_view path,
10661066
if (json_path.RefersToRootElement()) {
10671067
auto& db_slice = op_args.GetDbSlice();
10681068
auto res_it = db_slice.FindMutable(op_args.db_cntx, key, OBJ_JSON);
1069+
1070+
// For JSON.DEL, if key doesn't exist, return 0 instead of error
1071+
if (res_it.status() == OpStatus::KEY_NOTFOUND) {
1072+
return 0;
1073+
}
1074+
10691075
RETURN_ON_BAD_STATUS(res_it);
10701076

10711077
if (IsValid(res_it->it)) {

src/server/json_family_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3155,4 +3155,18 @@ TEST_F(JsonFamilyTest, ResetStringKeyWithSetGet) {
31553155
EXPECT_THAT(resp, R"({"a":"b"})");
31563156
}
31573157

3158+
TEST_F(JsonFamilyTest, DelNonExistingKey) {
3159+
auto resp = Run({"EXISTS", "nonexisting_key"});
3160+
EXPECT_THAT(resp, IntArg(0));
3161+
3162+
resp = Run({"JSON.DEL", "nonexisting_key", "."});
3163+
EXPECT_THAT(resp, IntArg(0));
3164+
3165+
resp = Run({"JSON.DEL", "nonexisting_key", "$"});
3166+
EXPECT_THAT(resp, IntArg(0));
3167+
3168+
resp = Run({"JSON.DEL", "nonexisting_key"});
3169+
EXPECT_THAT(resp, IntArg(0));
3170+
}
3171+
31583172
} // namespace dfly

0 commit comments

Comments
 (0)