From 50f530a05cd6cce8eaa971437fa84f8da2b92f4a Mon Sep 17 00:00:00 2001 From: TwinProduction Date: Sun, 9 May 2021 13:28:22 -0400 Subject: [PATCH] Fix #107: Correctly parse placeholder when [BODY] is an array --- core/condition.go | 2 +- core/condition_test.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/condition.go b/core/condition.go index e22621b3..7d2c3bb6 100644 --- a/core/condition.go +++ b/core/condition.go @@ -232,7 +232,7 @@ func sanitizeAndResolve(elements []string, result *Result) ([]string, []string) checkingForExistence = true element = strings.TrimSuffix(strings.TrimPrefix(element, HasFunctionPrefix), FunctionSuffix) } - resolvedElement, resolvedElementLength, err := jsonpath.Eval(strings.TrimPrefix(element, BodyPlaceholder+"."), result.body) + resolvedElement, resolvedElementLength, err := jsonpath.Eval(strings.TrimPrefix(strings.TrimPrefix(element, BodyPlaceholder), "."), result.body) if checkingForExistence { if err != nil { element = "false" diff --git a/core/condition_test.go b/core/condition_test.go index edd39021..c3697873 100644 --- a/core/condition_test.go +++ b/core/condition_test.go @@ -183,6 +183,13 @@ func TestCondition_evaluate(t *testing.T) { ExpectedSuccess: false, ExpectedOutput: "[BODY].user.firstName (john) == [BODY].user.lastName (doe)", }, + { + Name: "body-jsonpath-when-body-is-array", + Condition: Condition("[BODY][0].id == 1"), + Result: &Result{body: []byte("[{\"id\": 1}, {\"id\": 2}]")}, + ExpectedSuccess: true, + ExpectedOutput: "[BODY][0].id == 1", + }, { Name: "body-jsonpath-complex-int", Condition: Condition("[BODY].data.id == 1"),