fix(sfn): treat null as falsy in JSONPath negation (#3497)

Co-authored-by: xujiantop-crypto <265865031+xujiantop-crypto@users.noreply.github.com>
This commit is contained in:
XuJianandxujiantop-crypto authored and GitHub committed 2026-09-12 13:44:20 -04:00
1 parent 8e3bd63c15
commit c18c80e557
2 files changed
+196 -1

No files matched your search

@@ -3287,7 +3287,8 @@ public class AslExecutor {
private JsonNode resolveAdvancedJsonPath(String path, JsonNode root) {
try {
Object result = JsonPath.using(jsonPathConfiguration).parse(root).read(path);
String compatiblePath = normalizeJsonPathNullNegation(path);
Object result = JsonPath.using(jsonPathConfiguration).parse(root).read(compatiblePath);
return result instanceof JsonNode jsonNode ? jsonNode : objectMapper.valueToTree(result);
} catch (PathNotFoundException e) {
return MissingNode.getInstance();
@@ -3296,6 +3297,132 @@ public class AslExecutor {
}
}
/**
* Jayway treats {@code !@.key} as a non-existence check. Step Functions also considers an
* explicit null value falsy, so expand only simple negated path operands to include that case.
*/
private static String normalizeJsonPathNullNegation(String path) {
StringBuilder normalized = null;
int copiedThrough = 0;
char quote = 0;
boolean escaped = false;
for (int i = 0; i < path.length(); i++) {
char current = path.charAt(i);
if (quote != 0) {
if (escaped) {
escaped = false;
} else if (current == '\\') {
escaped = true;
} else if (current == quote) {
quote = 0;
}
continue;
}
if (current == '\'' || current == '"') {
quote = current;
continue;
}
if (current != '!' || !isUnaryJsonPathNegation(path, i)) {
continue;
}
int operandStart = i + 1;
while (operandStart < path.length() && Character.isWhitespace(path.charAt(operandStart))) {
operandStart++;
}
if (operandStart >= path.length()
|| (path.charAt(operandStart) != '@' && path.charAt(operandStart) != '$')) {
continue;
}
int operandEnd = jsonPathOperandEnd(path, operandStart);
if (operandEnd < 0) {
continue;
}
if (normalized == null) {
normalized = new StringBuilder(path.length() + 32);
}
normalized.append(path, copiedThrough, i)
.append('(')
.append(path, i, operandEnd)
.append(" || ")
.append(path, operandStart, operandEnd)
.append(" == null)");
copiedThrough = operandEnd;
i = operandEnd - 1;
}
return normalized == null ? path : normalized.append(path, copiedThrough, path.length()).toString();
}
private static boolean isUnaryJsonPathNegation(String path, int index) {
int previous = index - 1;
while (previous >= 0 && Character.isWhitespace(path.charAt(previous))) {
previous--;
}
return previous < 0 || "([?&|,".indexOf(path.charAt(previous)) >= 0;
}
private static int jsonPathOperandEnd(String path, int operandStart) {
int index = operandStart + 1;
boolean hasSegment = false;
while (index < path.length()) {
char current = path.charAt(index);
if (current == '.') {
int memberStart = ++index;
while (index < path.length() && !isJsonPathOperandDelimiter(path.charAt(index))) {
index++;
}
if (index == memberStart) {
return -1;
}
hasSegment = true;
} else if (current == '[') {
int bracketEnd = simpleJsonPathBracketEnd(path, index);
if (bracketEnd < 0) {
return -1;
}
index = bracketEnd;
hasSegment = true;
} else {
break;
}
}
if (index < path.length() && path.charAt(index) == '(') {
return -1;
}
return hasSegment ? index : -1;
}
private static boolean isJsonPathOperandDelimiter(char value) {
return Character.isWhitespace(value) || ".[]()&|=!<>,".indexOf(value) >= 0;
}
private static int simpleJsonPathBracketEnd(String path, int openBracket) {
char quote = 0;
boolean escaped = false;
for (int i = openBracket + 1; i < path.length(); i++) {
char current = path.charAt(i);
if (quote != 0) {
if (escaped) {
escaped = false;
} else if (current == '\\') {
escaped = true;
} else if (current == quote) {
quote = 0;
}
continue;
}
if (current == '\'' || current == '"') {
quote = current;
} else if (current == ']') {
return i + 1;
} else if (current == '[' || current == '?' || current == '(' || current == ')') {
return -1;
}
}
return -1;
}
private static FailStateException unresolvedPayloadTemplateReference(String path, String fieldKey,
JsonNode reportedInput) {
return new FailStateException("States.Runtime",
@@ -122,6 +122,74 @@ class AslExecutorPathIntrinsicsTest {
executor.resolvePath("$.list[?(@.keep == true)]", root));
}
@Test
void filterNegationTreatsExplicitNullAndMissingPropertiesAsFalsy() throws Exception {
JsonNode root = mapper.readTree("""
{"items":[
{"id":1,"session":null},
{"id":2,"session":{"token":"active"}},
{"id":3}
]}
""");
assertEquals(mapper.readTree("[{\"id\":1,\"session\":null},{\"id\":3}]"),
executor.resolvePath("$.items[?(!@.session)]", root));
}
@Test
void filterNegationWorksInsideCompoundPredicates() throws Exception {
JsonNode root = mapper.readTree("""
{"items":[
{"id":1,"student_programme":"A","archived_at":null},
{"id":2,"student_programme":"B"},
{"id":3,"student_programme":"C","archived_at":"2026-09-01"},
{"id":4,"archived_at":null}
]}
""");
assertEquals(mapper.readTree("""
[{"id":1,"student_programme":"A","archived_at":null},
{"id":2,"student_programme":"B"}]
"""),
executor.resolvePath("$.items[?(@.student_programme && !@.archived_at)]", root));
}
@Test
void filterNegationSupportsBracketQuotedMembers() throws Exception {
JsonNode root = mapper.readTree("""
{"items":[
{"id":1,"archived-at":null},
{"id":2,"archived-at":"2026-09-01"},
{"id":3}
]}
""");
assertEquals(mapper.readTree("[{\"id\":1,\"archived-at\":null},{\"id\":3}]"),
executor.resolvePath("$.items[?(!@['archived-at'])]", root));
}
@Test
void filterNegationCompatibilityDoesNotRewriteComparisonsOrStringLiterals() throws Exception {
JsonNode root = mapper.readTree("""
{"items":[
{"id":1,"session":null,"label":"!@.session"},
{"id":2,"session":"active","label":"other"},
{"id":3,"label":"!@.session"}
]}
""");
assertEquals(mapper.readTree("""
[{"id":2,"session":"active","label":"other"},
{"id":3,"label":"!@.session"}]
"""),
executor.resolvePath("$.items[?(@.session != null)]", root));
assertEquals(mapper.readTree("""
[{"id":1,"session":null,"label":"!@.session"},
{"id":3,"label":"!@.session"}]
"""),
executor.resolvePath("$.items[?(@.label == '!@.session')]", root));
}
@Test
void recursiveDescentPreservesAndFlattensNestedArrayMatches() throws Exception {
JsonNode root = mapper.readTree("{\"d\":{\"x\":{\"a\":[1,2]}}}");