mirror of
https://github.com/redis/go-redis.git
synced 2026-10-28 08:24:54 +08:00
fix(json): don't drop paths with nil options (#4185)
JSONGetWithArgs appended the paths inside the options != nil block, so with nil options it sent JSON.GET key without the paths and returned the whole document instead of the requested paths.
This commit is contained in:
1 parent
3639ef714b
commit
5097c0df45
2 files changed
+55
-3
No files matched your search
@@ -525,9 +525,9 @@ func (c cmdable) JSONGetWithArgs(ctx context.Context, key string, options *JSONG
|
||||
if options.Space != "" {
|
||||
args = append(args, "SPACE", options.Space)
|
||||
}
|
||||
for _, path := range paths {
|
||||
args = append(args, path)
|
||||
}
|
||||
}
|
||||
for _, path := range paths {
|
||||
args = append(args, path)
|
||||
}
|
||||
cmd := newJSONCmd(ctx, args...)
|
||||
_ = c(ctx, cmd)
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestJSONGetWithArgsArgs checks that JSONGetWithArgs sends the paths with
|
||||
// and without options.
|
||||
func TestJSONGetWithArgsArgs(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
options *JSONGetArgs
|
||||
paths []string
|
||||
want []interface{}
|
||||
}{
|
||||
{
|
||||
name: "nil_options_with_paths",
|
||||
paths: []string{"$.a", "$.b"},
|
||||
want: []interface{}{"JSON.GET", "key", "$.a", "$.b"},
|
||||
},
|
||||
{
|
||||
name: "nil_options_without_paths",
|
||||
want: []interface{}{"JSON.GET", "key"},
|
||||
},
|
||||
{
|
||||
name: "empty_options_with_paths",
|
||||
options: &JSONGetArgs{},
|
||||
paths: []string{"$.a"},
|
||||
want: []interface{}{"JSON.GET", "key", "$.a"},
|
||||
},
|
||||
{
|
||||
name: "options_with_paths",
|
||||
options: &JSONGetArgs{Indent: " ", Newline: "\n", Space: " "},
|
||||
paths: []string{"$.a"},
|
||||
want: []interface{}{"JSON.GET", "key", "INDENT", " ", "NEWLINE", "\n", "SPACE", " ", "$.a"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var captured Cmder
|
||||
cmd := captureCmdable(&captured).JSONGetWithArgs(ctx, "key", tt.options, tt.paths...)
|
||||
if !reflect.DeepEqual(cmd.Args(), tt.want) {
|
||||
t.Errorf("args mismatch\n got: %#v\nwant: %#v", cmd.Args(), tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user