From 57ef931d389baf9b5a5d6b3b05cf46af083ad5d8 Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Sat, 15 May 2021 22:38:13 -0400 Subject: [PATCH] Add TestEvalWithArrayOfValuesAndInvalidIndex --- jsonpath/jsonpath.go | 2 +- jsonpath/jsonpath_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/jsonpath/jsonpath.go b/jsonpath/jsonpath.go index 2bbf00fa..dedf306f 100644 --- a/jsonpath/jsonpath.go +++ b/jsonpath/jsonpath.go @@ -41,7 +41,7 @@ func extractValue(currentKey string, value interface{}) interface{} { tmp := strings.SplitN(currentKey, "[", 3) arrayIndex, err := strconv.Atoi(strings.Replace(tmp[1], "]", "", 1)) if err != nil { - return value + return nil } currentKey := tmp[0] // if currentKey contains only an index (i.e. [0] or 0) diff --git a/jsonpath/jsonpath_test.go b/jsonpath/jsonpath_test.go index 3e9063be..ccadf78f 100644 --- a/jsonpath/jsonpath_test.go +++ b/jsonpath/jsonpath_test.go @@ -84,6 +84,16 @@ func TestEvalWithArrayOfValues(t *testing.T) { } } +func TestEvalWithArrayOfValuesAndInvalidIndex(t *testing.T) { + path := "ids[wat]" + data := `{"ids": [1, 2]}` + + _, _, err := Eval(path, []byte(data)) + if err == nil { + t.Error("Expected an error") + } +} + func TestEvalWithRootArrayOfValues(t *testing.T) { path := "[1]" data := `[1, 2]`