From 8b7eb09506c9d3670b7989e5797814d9c47091f6 Mon Sep 17 00:00:00 2001 From: iko1 Date: Wed, 22 Feb 2023 00:36:15 +0200 Subject: [PATCH] fix(server): JSON.RESP path arg should be optional (#852) Signed-off-by: iko1 --- src/server/json_family.cc | 9 +++++++-- src/server/json_family_test.cc | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/server/json_family.cc b/src/server/json_family.cc index a11222b0d393..8edcb516deea 100644 --- a/src/server/json_family.cc +++ b/src/server/json_family.cc @@ -38,6 +38,8 @@ using JsonReplaceCb = function; using JsonReplaceVerify = std::function; using CI = CommandId; +static const char DefaultJsonPath[] = "$"; + namespace { inline OpStatus JsonReplaceVerifyNoOp() { @@ -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(path, ec); @@ -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); } diff --git a/src/server/json_family_test.cc b/src/server/json_family_test.cc index 9fb2734d54a8..a26b07ca382d 100644 --- a/src/server/json_family_test.cc +++ b/src/server/json_family_test.cc @@ -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"));