DynamoDB - raise exception for invalid parameter combination (#3755)

This commit is contained in:
Bert Blommers 2021-08-28 07:41:04 +01:00 committed by GitHub
parent 0317c502f0
commit 6a6a71ebff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 2 deletions

View File

@ -755,8 +755,12 @@ class DynamoHandler(BaseResponse):
return_values = self.body.get("ReturnValues", "NONE")
update_expression = self.body.get("UpdateExpression", "").strip()
attribute_updates = self.body.get("AttributeUpdates")
expression_attribute_names = self.body.get("ExpressionAttributeNames", {})
expression_attribute_values = self.body.get("ExpressionAttributeValues", {})
if update_expression and attribute_updates:
er = "com.amazonaws.dynamodb.v20111205#ValidationException"
return self.error(
er,
"Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {AttributeUpdates} Expression parameters: {UpdateExpression}",
)
# We need to copy the item in order to avoid it being modified by the update_item operation
existing_item = copy.deepcopy(self.dynamodb_backend.get_item(name, key))
if existing_item:

View File

@ -5834,6 +5834,34 @@ def test_get_item_for_non_existent_table_raises_error():
ex.value.response["Error"]["Message"].should.equal("Requested resource not found")
@mock_dynamodb2
def test_error_when_providing_expression_and_nonexpression_params():
client = boto3.client("dynamodb", "eu-central-1")
table_name = "testtable"
client.create_table(
TableName=table_name,
KeySchema=[{"AttributeName": "pkey", "KeyType": "HASH"},],
AttributeDefinitions=[{"AttributeName": "pkey", "AttributeType": "S"},],
BillingMode="PAY_PER_REQUEST",
)
with pytest.raises(ClientError) as ex:
client.update_item(
TableName=table_name,
Key={"pkey": {"S": "testrecord"}},
AttributeUpdates={
"test_field": {"Value": {"SS": ["test1", "test2"],}, "Action": "PUT"}
},
UpdateExpression="DELETE orders :order",
ExpressionAttributeValues={":order": {"SS": ["item"]}},
)
err = ex.value.response["Error"]
err["Code"].should.equal("ValidationException")
err["Message"].should.equal(
"Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {AttributeUpdates} Expression parameters: {UpdateExpression}"
)
@mock_dynamodb2
def test_attribute_item_delete():
name = "TestTable"