DynamoDB - error when persisting an empty number set (#5118)

This commit is contained in:
Bert Blommers 2022-05-11 09:14:43 +00:00 committed by GitHub
parent 749b543b7c
commit 08d705aa99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 17 deletions

View File

@ -81,22 +81,23 @@ def put_has_empty_keys(field_updates, table):
return False
def get_empty_str_error():
er = "com.amazonaws.dynamodb.v20111205#ValidationException"
return (
400,
{"server": "amazon.com"},
dynamo_json_dump(
{
"__type": er,
"message": (
"One or more parameter values were "
"invalid: An AttributeValue may not "
"contain an empty string"
),
}
),
)
def put_has_empty_attrs(field_updates, table):
# Example invalid attribute: [{'M': {'SS': {'NS': []}}}]
def _validate_attr(attr: dict):
if "NS" in attr and attr["NS"] == []:
return True
else:
return any(
[_validate_attr(val) for val in attr.values() if isinstance(val, dict)]
)
if table:
key_names = table.attribute_keys
attrs_to_check = [
val for attr, val in field_updates.items() if attr not in key_names
]
return any([_validate_attr(attr) for attr in attrs_to_check])
return False
class DynamoHandler(BaseResponse):
@ -352,7 +353,13 @@ class DynamoHandler(BaseResponse):
raise MockValidationException("Return values set to invalid value")
if put_has_empty_keys(item, self.dynamodb_backend.get_table(name)):
return get_empty_str_error()
raise MockValidationException(
"One or more parameter values were invalid: An AttributeValue may not contain an empty string"
)
if put_has_empty_attrs(item, self.dynamodb_backend.get_table(name)):
raise MockValidationException(
"One or more parameter values were invalid: An number set may not be empty"
)
overwrite = "Expected" not in self.body
if not overwrite:

View File

@ -579,3 +579,23 @@ def test_put_item_wrong_datatype():
err = exc.value.response["Error"]
err["Code"].should.equal("SerializationException")
err["Message"].should.equal("NUMBER_VALUE cannot be converted to String")
@mock_dynamodb
def test_put_item_empty_set():
client = boto3.client("dynamodb", region_name="us-east-1")
dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
client.create_table(
TableName="test-table",
KeySchema=[{"AttributeName": "Key", "KeyType": "HASH"}],
AttributeDefinitions=[{"AttributeName": "Key", "AttributeType": "S"}],
BillingMode="PAY_PER_REQUEST",
)
table = dynamodb.Table("test-table")
with pytest.raises(ClientError) as exc:
table.put_item(Item={"Key": "some-irrelevant_key", "attr2": {"SS": set([])}})
err = exc.value.response["Error"]
err["Code"].should.equal("ValidationException")
err["Message"].should.equal(
"One or more parameter values were invalid: An number set may not be empty"
)