fix(dynamodb): tolerate whitespace before a function's own parenthesis (#2510)

applySetClause/evaluateSetExpr gated list_append and if_not_exists on a
literal startsWith(functionName + "("), so "list_append (x, y)" (space
before the parenthesis) fell through to the "plain attribute reference"
branch instead of being recognized as a function call. The SET then
silently resolved to no value and left the target attribute unchanged -
no exception, no error, just a dropped write.

Real AWS DynamoDB's UpdateExpression grammar is whitespace-insensitive
around function calls, and this is exactly the form PynamoDB (a widely
used Python DynamoDB ORM) always emits, so this reliably data-loses
every list_append done through PynamoDB.

#1640 already fixed whitespace normalization between clause keywords and
between a function's arguments; this closes the remaining gap between a
function's name and its own opening parenthesis, which sits in a
different code path (the per-clause value evaluator, not the top-level
tokenizer) and wasn't covered by that fix's tests.

Fixes #2509
This commit is contained in:
Kumar Prabhat authored and GitHub committed 2026-08-25 23:48:06 -05:00
1 parent 859db2f352
commit e4b101d5da
2 files changed
+45 -3

No files matched your search

@@ -1766,7 +1766,7 @@ public class DynamoDbService implements ResourceProvider {
throw new AwsException("ValidationException",
"The parameter cannot be converted to a numeric value", 400);
}
} else if (valuePart.startsWith("if_not_exists(")) {
} else if (startsWithFunctionCall(valuePart, "if_not_exists")) {
// if_not_exists(attrRef, fallbackExpr) evaluates to:
// attrRef's current value — when attrRef exists in the item
// fallbackExpr — otherwise
@@ -1789,7 +1789,7 @@ public class DynamoDbService implements ResourceProvider {
setValueAtPath(item, attrPath, resolved, exprAttrNames);
}
}
} else if (valuePart.toLowerCase().startsWith("list_append(")) {
} else if (startsWithFunctionCall(valuePart.toLowerCase(), "list_append")) {
int open = valuePart.indexOf('(');
int close = valuePart.lastIndexOf(')');
if (open >= 0 && close > open) {
@@ -1867,7 +1867,7 @@ public class DynamoDbService implements ResourceProvider {
private JsonNode evaluateSetExpr(ObjectNode item, String expr,
JsonNode exprAttrNames, JsonNode exprAttrValues) {
if (expr.toLowerCase().startsWith("if_not_exists(")) {
if (startsWithFunctionCall(expr.toLowerCase(), "if_not_exists")) {
String[] args = extractFunctionArgs(expr);
if (args.length == 2) {
String checkAttr = resolveAttributeName(args[0].trim(), exprAttrNames);
@@ -2467,6 +2467,18 @@ public class DynamoDbService implements ResourceProvider {
return -1;
}
/**
* Whether {@code value} opens with a call to {@code functionName}, tolerating optional
* whitespace between the function name and the opening parenthesis. DynamoDB's
* UpdateExpression grammar is whitespace-insensitive there, but SDKs commonly emit the
* spaced form (e.g. PynamoDB always generates {@code "list_append (x, y)"}), which a plain
* {@code startsWith(functionName + "(")} rejects.
*/
private static boolean startsWithFunctionCall(String value, String functionName) {
return value.startsWith(functionName)
&& value.substring(functionName.length()).stripLeading().startsWith("(");
}
private String[] extractFunctionArgs(String funcCall) {
int open = funcCall.indexOf('(');
int close = funcCall.lastIndexOf(')');
@@ -159,6 +159,36 @@ class DynamoDbUpdateExpressionWhitespaceTest {
.body("Attributes.c.N", equalTo("6"));
}
/**
* Regression test for #2509: a space between a function name and its own opening
* parenthesis (e.g. {@code "list_append ("} rather than {@code "list_append("}) is
* accepted by real AWS and is exactly what PynamoDB always emits, but
* {@code applySetClause}/{@code evaluateSetExpr} gated on a literal
* {@code startsWith(functionName + "(")} check, so the spaced form fell through to the
* "plain attribute reference" branch and silently failed to resolve - the SET was a
* no-op with no error. Distinct from #1640 above: this whitespace sits between the
* function name and its own parenthesis, not between clause keywords or a function's
* arguments, so #1640's tokenizer-level normalization didn't reach it.
*/
@Test
void spaceBeforeFunctionParenthesisIsAccepted() {
createAndSeed("UpdWsFuncSpace", """
{"pk":{"S":"x"},"l":{"L":[{"S":"one"}]},"m":{"S":"present"}}""");
given().header("X-Amz-Target", "DynamoDB_20120810.UpdateItem").contentType(CT).body("""
{"TableName":"UpdWsFuncSpace","Key":{"pk":{"S":"x"}},
"UpdateExpression":"SET #l = list_append (#l, :more), #m = if_not_exists (#missing, :fallback)",
"ReturnValues":"ALL_NEW",
"ExpressionAttributeNames":{"#l":"l","#m":"m","#missing":"missing"},
"ExpressionAttributeValues":{":more":{"L":[{"S":"two"}]},":fallback":{"S":"unused"}}}
""")
.when().post("/").then()
.statusCode(200)
.body("Attributes.l.L.size()", equalTo(2))
.body("Attributes.l.L[0].S", equalTo("one"))
.body("Attributes.l.L[1].S", equalTo("two"))
.body("Attributes.m.S", equalTo("unused"));
}
@Test
void invalidLeadingKeywordIsStillRejected() {
createAndSeed("UpdWsBad");