Skip to content

Commit

Permalink
fix: string escaping comparisson with filters (#3951)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathnogueira authored Aug 5, 2024
1 parent d4d4318 commit d3be6ca
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions server/expression/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func compare(comparatorName string, leftValue, rightValue value.Value) error {
return compareArrayContains(leftValue, rightValue)
}

return comparatorFunction.Compare(rightValue.String(), leftValue.String())
return comparatorFunction.Compare(rightValue.UnescappedString(), leftValue.UnescappedString())
}

func compareArrayContains(array, expected value.Value) error {
Expand All @@ -34,7 +34,7 @@ func compareArrayContains(array, expected value.Value) error {
}

for _, item := range array.Items {
if err = equalComparator.Compare(item.Value, expected.String()); err == nil {
if err = equalComparator.Compare(item.Value, expected.UnescappedString()); err == nil {
return nil
}
}
Expand Down
6 changes: 2 additions & 4 deletions server/expression/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ func (e Executor) resolveTerm(term *Term) (value.Value, error) {
strValue = fmt.Sprintf(term.Str.Text, stringArgs...)
}

str := value.NewFromString(strValue)
return value.NewFromString(str.UnescappedString()), nil
return value.NewFromString(strValue), nil
}

return value.Nil, fmt.Errorf("empty term")
Expand Down Expand Up @@ -243,8 +242,7 @@ func (e Executor) resolveAttribute(attribute *Attribute) (value.Value, error) {
return value.Nil, resolutionError(err)
}

str := value.NewFromString(attributeValue)
return value.NewFromString(str.UnescappedString()), nil
return value.NewFromString(attributeValue), nil
}

func (e Executor) resolveEnvironment(environment *Environment) (value.Value, error) {
Expand Down
5 changes: 5 additions & 0 deletions server/expression/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func TestBasicExpressionExecution(t *testing.T) {
},
},
},
{
Name: "escaped_strings_in_json_must_work_with_json_path",
Query: `'{ "message": "this should \"work\"", "anotherValue": 12 }' | json_path '$.message' = 'this should "work"'`,
ShouldPass: true,
},
}

executeTestCases(t, testCases)
Expand Down
6 changes: 6 additions & 0 deletions server/expression/filters/json_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func TestJSONPath(t *testing.T) {
Query: `$.array[*]..['id', 'name']`,
ExpectedOutput: `[38, "Tracetest", 39, "Kusk"]`,
},
{
Name: "escaped_json_should_work",
JSON: `{ "message": "\"rails g model Ping user:references location:point\", then migrate." }`,
Query: `$.message`,
ExpectedOutput: `"rails g model Ping user:references location:point", then migrate.`,
},
}

for _, testCase := range testCases {
Expand Down

0 comments on commit d3be6ca

Please sign in to comment.