1
0
Fork 0
mirror of https://github.com/TwiN/gatus.git synced 2024-12-14 11:58:04 +00:00

fix(jsonpath): Prevent panic when body is "null"

Resolves #718
This commit is contained in:
TwiN 2024-04-01 20:03:02 -04:00
parent ceb2c7884f
commit 979d467e36
2 changed files with 30 additions and 2 deletions

View file

@ -259,6 +259,27 @@ func TestCondition_evaluate(t *testing.T) {
ExpectedSuccess: true, ExpectedSuccess: true,
ExpectedOutput: "[BODY][0].id == 1", ExpectedOutput: "[BODY][0].id == 1",
}, },
{
Name: "body-jsonpath-when-body-has-null-parameter",
Condition: Condition("[BODY].data == OK"),
Result: &Result{Body: []byte(`{"data": null}"`)},
ExpectedSuccess: false,
ExpectedOutput: "[BODY].data (INVALID) == OK",
},
{
Name: "body-jsonpath-when-body-has-array-with-null",
Condition: Condition("[BODY].items[0] == OK"),
Result: &Result{Body: []byte(`{"items": [null, null]}"`)},
ExpectedSuccess: false,
ExpectedOutput: "[BODY].items[0] (INVALID) == OK",
},
{
Name: "body-jsonpath-when-body-is-null",
Condition: Condition("[BODY].data == OK"),
Result: &Result{Body: []byte(`null`)},
ExpectedSuccess: false,
ExpectedOutput: "[BODY].data (INVALID) == OK",
},
{ {
Name: "body-jsonpath-when-body-is-array-but-actual-body-is-not", Name: "body-jsonpath-when-body-is-array-but-actual-body-is-not",
Condition: Condition("[BODY][0].name == test"), Condition: Condition("[BODY][0].name == test"),

View file

@ -117,8 +117,15 @@ func extractValue(currentKey string, value interface{}) interface{} {
} }
if valueAsSlice, ok := value.([]interface{}); ok { if valueAsSlice, ok := value.([]interface{}); ok {
// If the type is a slice, return it // If the type is a slice, return it
// This happens when the body (value) is a JSON array
return valueAsSlice return valueAsSlice
} }
// otherwise, it's a map if valueAsMap, ok := value.(map[string]interface{}); ok {
return value.(map[string]interface{})[currentKey] // If the value is a map, then we get the currentKey from that map
// This happens when the body (value) is a JSON object
return valueAsMap[currentKey]
}
// If the value is neither a map, nor a slice, nor an index, then we cannot retrieve the currentKey
// from said value. This usually happens when the body (value) is null.
return value
} }