Skip to content

Commit

Permalink
DynamoDB: update_item() now validates an empty ExpressionAttributeVal…
Browse files Browse the repository at this point in the history
…ues-parameter (#7964)
  • Loading branch information
bblommers authored Aug 10, 2024
1 parent 33ce9d9 commit 5f94843
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion moto/dynamodb/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,11 @@ def update_item(self) -> str:
return dynamo_json_dump(item_dict)

def _get_expr_attr_values(self) -> Dict[str, Dict[str, str]]:
values = self.body.get("ExpressionAttributeValues", {})
values = self.body.get("ExpressionAttributeValues")
if values is None:
return {}
if len(values) == 0:
raise MockValidationException("ExpressionAttributeValues must not be empty")
for key in values.keys():
if not key.startswith(":"):
raise MockValidationException(
Expand Down
19 changes: 19 additions & 0 deletions tests/test_dynamodb/test_dynamodb_update_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,22 @@ def test_update_item_add_empty_set(table_name=None):
assert dynamodb.scan(TableName=table_name)["Items"] == [
{"pk": {"S": "foo"}, "stringset": {"SS": ["item1"]}}
]


@pytest.mark.aws_verified
@dynamodb_aws_verified()
def test_update_item_with_empty_values(table_name=None):
dynamodb = boto3.client("dynamodb", "us-east-1")

dynamodb.put_item(TableName=table_name, Item={"pk": {"S": "foo"}})
with pytest.raises(ClientError) as exc:
dynamodb.update_item(
TableName=table_name,
Key={"pk": {"S": "foo"}},
UpdateExpression="SET #d = :s",
ExpressionAttributeNames={"#d": "d"},
ExpressionAttributeValues={},
)
err = exc.value.response["Error"]
assert err["Code"] == "ValidationException"
assert err["Message"] == "ExpressionAttributeValues must not be empty"

0 comments on commit 5f94843

Please sign in to comment.