Skip to content

Commit

Permalink
fix(server): JSON.RESP path arg should be optional (dragonflydb#852)
Browse files Browse the repository at this point in the history
Signed-off-by: iko1 <me@remotecpp.dev>
  • Loading branch information
iko1 committed Feb 21, 2023
1 parent e52b0f4 commit 8b7eb09
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/server/json_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ using JsonReplaceCb = function<void(const string&, JsonType&)>;
using JsonReplaceVerify = std::function<OpStatus()>;
using CI = CommandId;

static const char DefaultJsonPath[] = "$";

namespace {

inline OpStatus JsonReplaceVerifyNoOp() {
Expand Down Expand Up @@ -1004,7 +1006,10 @@ void JsonFamily::Set(CmdArgList args, ConnectionContext* cntx) {

void JsonFamily::Resp(CmdArgList args, ConnectionContext* cntx) {
string_view key = ArgS(args, 1);
string_view path = ArgS(args, 2);
string_view path = DefaultJsonPath;
if (args.size() > 2) {
path = ArgS(args, 2);
}

error_code ec;
JsonExpression expression = jsonpath::make_expression<JsonType>(path, ec);
Expand Down Expand Up @@ -1661,7 +1666,7 @@ void JsonFamily::Register(CommandRegistry* registry) {
ArrAppend);
*registry << CI{"JSON.ARRINDEX", CO::READONLY | CO::FAST, -4, 1, 1, 1}.HFUNC(ArrIndex);
*registry << CI{"JSON.DEBUG", CO::READONLY | CO::FAST, -2, 1, 1, 1}.HFUNC(Debug);
*registry << CI{"JSON.RESP", CO::READONLY | CO::FAST, 3, 1, 1, 1}.HFUNC(Resp);
*registry << CI{"JSON.RESP", CO::READONLY | CO::FAST, -2, 1, 1, 1}.HFUNC(Resp);
*registry << CI{"JSON.SET", CO::WRITE | CO::DENYOOM | CO::FAST, 4, 1, 1, 1}.HFUNC(Set);
}

Expand Down
3 changes: 3 additions & 0 deletions src/server/json_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,9 @@ TEST_F(JsonFamilyTest, Resp) {
auto resp = Run({"JSON.SET", "json", ".", PhonebookJson});
ASSERT_THAT(resp, "OK");

resp = Run({"JSON.RESP", "json"});
ASSERT_EQ(RespExpr::ARRAY, resp.type);

resp = Run({"JSON.RESP", "json", "$.address.*"});
ASSERT_EQ(RespExpr::ARRAY, resp.type);
EXPECT_THAT(resp.GetVec(), ElementsAre("New York", "NY", "21 2nd Street", "10021-3100"));
Expand Down

0 comments on commit 8b7eb09

Please sign in to comment.