mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(eventbridge): match nested patterns against arrays of objects (#7849)
Narrow the change to the two array cases that element-by-element matching
of scalar arrays does not cover, and drop the scalar handling in
matchesSingleElement.
A nested pattern such as {"detail":{"items":{"id":["a"]}}} never matched
{"items":[{"id":"a"}]}, because matchesDetailNode looked the field up on
the array itself. A nested pattern now matches an array when any element
matches it, as PipesFilterMatcher.matchesNested does.
An empty array is treated as an absent field, so {"exists":false} matches
it and {"exists":true} does not.
The per-case TestEventPattern requests are replaced by one parameterized
test in EventBridgeServiceTest.
This commit is contained in:
1 parent
8e4c1994b7
commit
e607b1a790
2 files changed
+31
-2
No files matched your search
@@ -1253,6 +1253,18 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
}
|
||||
}
|
||||
|
||||
private boolean matchesNestedDetail(JsonNode actual, JsonNode pattern) {
|
||||
if (actual.isArray()) {
|
||||
for (JsonNode element : actual) {
|
||||
if (matchesDetailNode(element, pattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return matchesDetailNode(actual, pattern);
|
||||
}
|
||||
|
||||
private boolean matchesDetailNode(JsonNode actual, JsonNode pattern) {
|
||||
JsonNode orClauses = pattern.get("$or");
|
||||
if (orClauses != null && orClauses.isArray()) {
|
||||
@@ -1276,7 +1288,9 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
JsonNode expected = field.getValue();
|
||||
JsonNode actualField = actual.get(field.getKey());
|
||||
if (expected.isArray()) {
|
||||
if (!matchesArrayField(expected, actualField)) {
|
||||
JsonNode leaf = actualField != null && actualField.isArray() && actualField.isEmpty()
|
||||
? null : actualField;
|
||||
if (!matchesArrayField(expected, leaf)) {
|
||||
return false;
|
||||
}
|
||||
} else if (expected.isObject()) {
|
||||
@@ -1291,7 +1305,7 @@ public class EventBridgeService implements ResourceProvider, StoredResourceArns
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!matchesDetailNode(nestedActual, expected)) {
|
||||
if (!matchesNestedDetail(nestedActual, expected)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1257,6 +1257,21 @@ class EventBridgeServiceTest {
|
||||
"{\"detail\":{\"a\":{\"b\":{\"c\":[\"wrong\"]}}}}"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiter = '|', value = {
|
||||
"{\"detail\":{\"items\":{\"id\":[\"a\"]}}}|{\"items\":[{\"id\":\"a\"}]}|true",
|
||||
"{\"detail\":{\"items\":{\"id\":[\"a\"]}}}|{\"items\":[{\"id\":\"b\"},{\"id\":\"a\"}]}|true",
|
||||
"{\"detail\":{\"items\":{\"id\":[\"a\"]}}}|{\"items\":[{\"id\":\"b\"},{\"id\":\"c\"}]}|false",
|
||||
"{\"detail\":{\"tags\":[{\"exists\":false}]}}|{\"tags\":[]}|true",
|
||||
"{\"detail\":{\"tags\":[{\"exists\":true}]}}|{\"tags\":[]}|false",
|
||||
"{\"detail\":{\"tags\":[{\"exists\":false}]}}|{\"tags\":[\"a\"]}|false",
|
||||
"{\"detail\":{\"tags\":[{\"exists\":true}]}}|{\"tags\":[\"a\"]}|true"
|
||||
})
|
||||
void matchesPatternAgainstArrayDetail(String pattern, String detail, boolean expected) {
|
||||
Map<String, Object> event = Map.of("Source", "my.app", "Detail", detail);
|
||||
assertEquals(expected, service.matchesPattern(event, pattern));
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesPatternTopLevelOr_firstBranchMatches() {
|
||||
Map<String, Object> event = Map.of("Source", "aws.ec2", "DetailType", "Other");
|
||||
|
||||
Reference in new issue
Block a user