Skip to content

Commit f4edaae

Browse files
nic-gibsonofekshenawa
authored andcommitted
Fix JSON nil response handling
Ensure that JSON.GET returns redis.Nil for missing keys/paths, making it consistent with other Redis commands. - Restore proper nil detection logic in JSONCmd.readReply - Add comprehensive test coverage for JSON nil scenarios - Handle both non-existent keys and non-existent paths consistently - Distinguish between empty arrays and nil responses - Add documentation for Val() and Expanded() methods Original work and problem identification by Nic Gibson. Enhanced implementation with comprehensive testing and fixes for the broken nil detection logic. Fixes #2987
1 parent 1d6c453 commit f4edaae

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

json.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func (cmd *JSONCmd) SetVal(val string) {
8282
cmd.val = val
8383
}
8484

85+
// Val returns the result of the JSON.GET command as a string.
8586
func (cmd *JSONCmd) Val() string {
8687
if len(cmd.val) == 0 && cmd.expanded != nil {
8788
val, err := json.Marshal(cmd.expanded)
@@ -100,6 +101,7 @@ func (cmd *JSONCmd) Result() (string, error) {
100101
return cmd.Val(), cmd.Err()
101102
}
102103

104+
// Expanded returns the result of the JSON.GET command as unmarshalled JSON.
103105
func (cmd *JSONCmd) Expanded() (interface{}, error) {
104106
if len(cmd.val) != 0 && cmd.expanded == nil {
105107
err := json.Unmarshal([]byte(cmd.val), &cmd.expanded)
@@ -152,6 +154,7 @@ func (cmd *JSONCmd) readReply(rd *proto.Reader) error {
152154
return err
153155
} else if str == "" || err == Nil {
154156
cmd.val = ""
157+
return Nil
155158
} else {
156159
cmd.val = str
157160
}

json_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ var _ = Describe("JSON Commands", Label("json"), func() {
142142
resArr, err := client.JSONArrIndex(ctx, "doc1", "$.store.book[?(@.price<10)].size", 20).Result()
143143
Expect(err).NotTo(HaveOccurred())
144144
Expect(resArr).To(Equal([]int64{1, 2}))
145+
146+
_, err = client.JSONGet(ctx, "this-key-does-not-exist", "$").Result()
147+
Expect(err).To(HaveOccurred())
148+
Expect(err).To(BeIdenticalTo(redis.Nil))
145149
})
146150

147151
It("should JSONArrInsert", Label("json.arrinsert", "json"), func() {

0 commit comments

Comments
 (0)