fix(dynamodb): reject undefined expression attribute placeholders (#4398)

* fix(dynamodb): reject undefined expression attribute placeholders

PutItem/DeleteItem/UpdateItem ConditionExpression, UpdateItem's own
UpdateExpression, Query KeyConditionExpression, Query/Scan FilterExpression,
and TransactWriteItems' ConditionCheck/Put/Delete/Update ConditionExpression
and Update UpdateExpression accepted a #name or :value placeholder with no
matching ExpressionAttributeNames/Values entry, instead of rejecting it with
AWS's ValidationException. Writes fell through to a false
ConditionalCheckFailedException (or, inside a transaction, a
TransactionCanceledException with a false ConditionalCheckFailed reason),
reads silently evaluated the condition as non-matching (Query) or ignored it
entirely (Scan), and an undefined #name in an UpdateExpression's SET clause
was silently stored as an attribute literally named "#n" instead of being
rejected (standalone UpdateItem and inside a transaction alike). An undefined
#name in KeyConditionExpression additionally surfaced the wrong error, "Query
condition missed key schema element", because attribute resolution silently
left the alias unresolved.

Generalizes the pattern already used for UpdateExpression's undefined :value
check and Query's FilterExpression undefined #name check into a shared
requireDefinedTokens helper, applied to the remaining expression/handler
combinations, and built on the same ordered token scanner extractHashTokens
and extractColonTokens already used. The helper reports whichever undefined
token appears first in the text; in UpdateItem the ConditionExpression check
now runs before the unused-EAN/EAV checks rather than after.
ProjectionExpression's undefined #name check and KeyConditionExpression's
undefined :value check were already correct and are untouched.

* fix(dynamodb): check undefined placeholders before semantic validation

ExpressionEvaluator.validateExpression's semantic checks (contains()'s
distinct-operand rule, begins_with()'s operand type, BETWEEN's bound
ordering) ran before requireDefinedTokens at every call site, so an
undefined #name/:value could surface the wrong error, e.g.
contains(#missing, #missing) reported the distinct-operand error instead
of the undefined name. Verified against amazon/dynamodb-local:3.3.1 that
DynamoDB reports the undefined placeholder first for contains(), begins_with()
type mismatch, and BETWEEN bound ordering, while a syntax error or redundant
parentheses still outrank an undefined placeholder.

Splits validateExpression into validateSyntax (tokenize/parse/redundant-
parentheses, unchanged position) and validateSemantics (the checks above),
and reorders each handler to run requireDefinedTokens between them.
validateExpression itself is untouched and still used as-is by
DynamoDbVectorSearch's SearchConditionExpression.

In UpdateItem, ConditionExpression's semantic check must also outrank the
unused-EAN/EAV checks (#2893), not just requireDefinedTokens: moved
validateSemantics to run immediately after requireDefinedTokens(conditionExpression)
in both the UpdateExpression-present and UpdateExpression-absent branches,
ahead of checkUnusedEan/checkUnusedEav. Verified the same ordering already
held in PutItem, DeleteItem, Query and Scan (their unused checks already ran
after validateSemantics), and that TransactWriteItems has no unused-EAN/EAV
check to conflict with.

Also adds braces to the unbraced conditionals in the token scanner added by
the previous commit, per AGENTS.md.

* fix(dynamodb): keep projection validation after filter semantics

The syntax/semantics split moved FilterExpression's undefined-token and
semantic checks below ProjectionExpression's in Query and Scan, so a
Query/Scan with FilterExpression "contains(a, a)" and an undefined #name in
ProjectionExpression now reports the projection error instead of the filter
one. Verified against amazon/dynamodb-local:3.3.1: FilterExpression's checks
outrank ProjectionExpression's, which in turn outrank
KeyConditionExpression's (unchanged, and already correctly ordered).
This commit is contained in:
Niklas Herder authored and GitHub committed 2026-09-28 12:13:36 -04:00
1 parent f40402fa93
commit 5ec18c507c
3 files changed
+970 -44

No files matched your search

@@ -4805,6 +4805,851 @@ given()
deleteTable(tableName);
}
// A filter's semantic error is reported before an undefined #name in the projection.
@Test
void filterSemanticErrorWinsOverUndefinedProjectionName() {
String tableName = "FilterVsProjectionTable";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
given()
.header("X-Amz-Target", "DynamoDB_20120810.Query")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeyConditionExpression": "pk = :pk",
"FilterExpression": "contains(a, a)",
"ProjectionExpression": "#p",
"ExpressionAttributeValues": {":pk": {"S": "a"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: The first operand must be distinct "
+ "from the remaining operands for this operator or function; operator: contains, "
+ "first operand: [a]"));
given()
.header("X-Amz-Target", "DynamoDB_20120810.Scan")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"FilterExpression": "contains(a, a)",
"ProjectionExpression": "#p"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: The first operand must be distinct "
+ "from the remaining operands for this operator or function; operator: contains, "
+ "first operand: [a]"));
deleteTable(tableName);
}
// An undefined #name/:value in ConditionExpression must reject, not false-fail the condition.
@Test
void undefinedTokensInConditionExpressionAreRejected() {
String tableName = "ConditionExprTokenTable";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
// PutItem: ConditionExpression references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "sk = :v"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// PutItem: ConditionExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = sk"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Both undefined; the textually-first token (:v) is reported, not names before values.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": ":v = #n"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// The undefined name must be reported ahead of contains()'s distinct-operand semantic error.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "contains(#missing, #missing)"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #missing"));
// DeleteItem: ConditionExpression references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "sk = :v"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// DeleteItem: ConditionExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = sk"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// UpdateItem: valid UpdateExpression, but ConditionExpression is undefined.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET payload = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}},
"ConditionExpression": "sk = :v"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// UpdateItem: ConditionExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET payload = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}},
"ConditionExpression": "#n = sk"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Undefined :v must win over the unused-EAV check for the stray :z.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET payload = :d",
"ConditionExpression": "sk = :v",
"ExpressionAttributeValues": {":d": {"S": "x"}, ":z": {"S": "unused"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// contains()'s distinct-operand error must win over the unused-EAN check for the stray #z.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET payload = :d",
"ConditionExpression": "contains(#m, #m)",
"ExpressionAttributeNames": {"#m": "sk", "#z": "unused"},
"ExpressionAttributeValues": {":d": {"S": "x"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: The first operand must be distinct "
+ "from the remaining operands for this operator or function; operator: contains, "
+ "first operand: [sk]"));
// Edge case: nested path #a.#b, only #a defined.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "attribute_not_exists(#a.#b)",
"ExpressionAttributeNames": {"#a": "payload"}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #b"));
// Edge case: the same undefined token used twice must still report once, not crash.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "sk = :v OR pk = :v"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// Control: a different token than the one supplied is missing and must be named.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = :v",
"ExpressionAttributeNames": {"#n": "sk"},
"ExpressionAttributeValues": {":other": {"S": "x"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// Control: a fully-defined ConditionExpression is still accepted; seed the item first.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}}
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = :v",
"ExpressionAttributeNames": {"#n": "sk"},
"ExpressionAttributeValues": {":v": {"S": "b"}}
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
deleteTable(tableName);
}
// Query/Scan must reject an undefined #name/:value, never silently treat it as "no match".
@Test
void undefinedTokensInKeyConditionAndFilterExpressionsAreRejected() {
String tableName = "QueryScanTokenTable";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
// Must be the real cause, not "Query condition missed key schema element".
given()
.header("X-Amz-Target", "DynamoDB_20120810.Query")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeyConditionExpression": "#n = :pk",
"ExpressionAttributeValues": {":pk": {"S": "a"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid KeyConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Must not silently evaluate as "no match" and return an empty page.
given()
.header("X-Amz-Target", "DynamoDB_20120810.Query")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeyConditionExpression": "pk = :pk",
"FilterExpression": "payload = :d",
"ExpressionAttributeValues": {":pk": {"S": "a"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Scan: FilterExpression references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.Scan")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"FilterExpression": "payload = :d"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Scan: FilterExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.Scan")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"FilterExpression": "#n = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Edge case: :p is a function argument, begins_with(#n, :p), not a bare operand.
given()
.header("X-Amz-Target", "DynamoDB_20120810.Scan")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"FilterExpression": "begins_with(#n, :p)",
"ExpressionAttributeNames": {"#n": "payload"}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid FilterExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :p"));
deleteTable(tableName);
}
// UpdateExpression must reject an undefined #name the same way it already rejects :value.
@Test
void undefinedTokensInUpdateExpressionAreRejected() {
String tableName = "UpdateExprTokenTable";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
// The UpdateExpression itself references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET notes = :d"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Previously this silently stored an attribute literally named "#n".
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET #n = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}}
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Both undefined; #n comes first in the text and is reported.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET #n = :d"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// :d is inside list_append's args but still comes before #n in the text.
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET notes = list_append(:d, #n)"
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Control: a fully-defined UpdateExpression #name is still accepted (no regression).
given()
.header("X-Amz-Target", "DynamoDB_20120810.UpdateItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET #n = :d",
"ExpressionAttributeNames": {"#n": "notes"},
"ExpressionAttributeValues": {":d": {"S": "x"}}
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
deleteTable(tableName);
}
// An undefined token must reject, never fall through to a false ConditionalCheckFailed.
@Test
void undefinedTokensInTransactWriteItemsAreRejected() {
String tableName = "TransactTokenTable";
given()
.header("X-Amz-Target", "DynamoDB_20120810.CreateTable")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"KeySchema": [
{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}
],
"AttributeDefinitions": [
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"}
],
"BillingMode": "PAY_PER_REQUEST"
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
// ConditionCheck's ConditionExpression references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"ConditionCheck": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "sk = :v"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// ConditionCheck's ConditionExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"ConditionCheck": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = sk"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Put's ConditionExpression references an undefined :value.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Put": {
"TableName": "%s",
"Item": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "sk = :v"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// Delete's ConditionExpression references an undefined #name.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Delete": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"ConditionExpression": "#n = sk"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Update's ConditionExpression is undefined despite an otherwise-valid UpdateExpression.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Update": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET notes = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}},
"ConditionExpression": "sk = :v"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid ConditionExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :v"));
// Previously this silently no-op'd instead of erroring.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Update": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET notes = :d"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Previously this silently stored an attribute literally named "#n".
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Update": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET #n = :d",
"ExpressionAttributeValues": {":d": {"S": "x"}}
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute name "
+ "used in the document path is not defined; attribute name: #n"));
// Both undefined; UpdateExpression's own token is reported first.
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Update": {
"TableName": "%s",
"Key": {"pk": {"S": "a"}, "sk": {"S": "b"}},
"UpdateExpression": "SET notes = :d",
"ConditionExpression": "sk = :v"
}
}]
}
""".formatted(tableName))
.when().post("/")
.then()
.statusCode(400)
.body("__type", containsString("ValidationException"))
.body("message", equalTo("Invalid UpdateExpression: An expression attribute value "
+ "used in expression is not defined; attribute value: :d"));
// Control: a fully-defined transaction is still accepted; seed the item first.
given()
.header("X-Amz-Target", "DynamoDB_20120810.PutItem")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TableName": "%s",
"Item": {"pk": {"S": "txn-control"}, "sk": {"S": "b"}}
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
given()
.header("X-Amz-Target", "DynamoDB_20120810.TransactWriteItems")
.contentType(DYNAMODB_CONTENT_TYPE)
.body("""
{
"TransactItems": [{
"Update": {
"TableName": "%s",
"Key": {"pk": {"S": "txn-control"}, "sk": {"S": "b"}},
"UpdateExpression": "SET #n = :d",
"ExpressionAttributeNames": {"#n": "notes"},
"ConditionExpression": "sk = :v",
"ExpressionAttributeValues": {":v": {"S": "b"}, ":d": {"S": "x"}}
}
}]
}
""".formatted(tableName))
.when().post("/")
.then().statusCode(200);
deleteTable(tableName);
}
private void deleteTable(String tableName) {
given()
.header("X-Amz-Target", "DynamoDB_20120810.DeleteTable")