Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dynamodb): tolerate whitespace before a function's own parenthesis (#2510)
Kumar Prabhate4b101d5dab2
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.javaindex 92f0ff3f5e..f0aee01670 100644--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbService.java@@ -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(')');diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbUpdateExpressionWhitespaceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbUpdateExpressionWhitespaceTest.javaindex 175ae3acfb..29cdb77200 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbUpdateExpressionWhitespaceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbUpdateExpressionWhitespaceTest.java@@ -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"));+ }+@Testvoid invalidLeadingKeywordIsStillRejected() {createAndSeed("UpdWsBad");