Merge pull request #1745 from cm-iwata/fix_upditem_with_emptystring
BugFix dynamodb2 update_item with empty string Fixes#1744
This commit is contained in:
commit
6aa753af6f
@ -20,6 +20,17 @@ def has_empty_keys_or_values(_dict):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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')}
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
class DynamoHandler(BaseResponse):
|
class DynamoHandler(BaseResponse):
|
||||||
|
|
||||||
def get_endpoint_name(self, headers):
|
def get_endpoint_name(self, headers):
|
||||||
@ -174,14 +185,7 @@ class DynamoHandler(BaseResponse):
|
|||||||
item = self.body['Item']
|
item = self.body['Item']
|
||||||
|
|
||||||
if has_empty_keys_or_values(item):
|
if has_empty_keys_or_values(item):
|
||||||
er = 'com.amazonaws.dynamodb.v20111205#ValidationException'
|
return get_empty_str_error()
|
||||||
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')}
|
|
||||||
))
|
|
||||||
|
|
||||||
overwrite = 'Expected' not in self.body
|
overwrite = 'Expected' not in self.body
|
||||||
if not overwrite:
|
if not overwrite:
|
||||||
@ -523,6 +527,7 @@ class DynamoHandler(BaseResponse):
|
|||||||
return dynamo_json_dump(item_dict)
|
return dynamo_json_dump(item_dict)
|
||||||
|
|
||||||
def update_item(self):
|
def update_item(self):
|
||||||
|
|
||||||
name = self.body['TableName']
|
name = self.body['TableName']
|
||||||
key = self.body['Key']
|
key = self.body['Key']
|
||||||
update_expression = self.body.get('UpdateExpression')
|
update_expression = self.body.get('UpdateExpression')
|
||||||
@ -533,6 +538,9 @@ class DynamoHandler(BaseResponse):
|
|||||||
'ExpressionAttributeValues', {})
|
'ExpressionAttributeValues', {})
|
||||||
existing_item = self.dynamodb_backend.get_item(name, key)
|
existing_item = self.dynamodb_backend.get_item(name, key)
|
||||||
|
|
||||||
|
if has_empty_keys_or_values(expression_attribute_values):
|
||||||
|
return get_empty_str_error()
|
||||||
|
|
||||||
if 'Expected' in self.body:
|
if 'Expected' in self.body:
|
||||||
expected = self.body['Expected']
|
expected = self.body['Expected']
|
||||||
else:
|
else:
|
||||||
|
@ -201,6 +201,48 @@ def test_item_add_empty_string_exception():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@requires_boto_gte("2.9")
|
||||||
|
@mock_dynamodb2
|
||||||
|
def test_update_item_with_empty_string_exception():
|
||||||
|
name = 'TestTable'
|
||||||
|
conn = boto3.client('dynamodb',
|
||||||
|
region_name='us-west-2',
|
||||||
|
aws_access_key_id="ak",
|
||||||
|
aws_secret_access_key="sk")
|
||||||
|
conn.create_table(TableName=name,
|
||||||
|
KeySchema=[{'AttributeName':'forum_name','KeyType':'HASH'}],
|
||||||
|
AttributeDefinitions=[{'AttributeName':'forum_name','AttributeType':'S'}],
|
||||||
|
ProvisionedThroughput={'ReadCapacityUnits':5,'WriteCapacityUnits':5})
|
||||||
|
|
||||||
|
conn.put_item(
|
||||||
|
TableName=name,
|
||||||
|
Item={
|
||||||
|
'forum_name': { 'S': 'LOLCat Forum' },
|
||||||
|
'subject': { 'S': 'Check this out!' },
|
||||||
|
'Body': { 'S': 'http://url_to_lolcat.gif'},
|
||||||
|
'SentBy': { 'S': "test" },
|
||||||
|
'ReceivedTime': { 'S': '12/9/2011 11:36:03 PM'},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
with assert_raises(ClientError) as ex:
|
||||||
|
conn.update_item(
|
||||||
|
TableName=name,
|
||||||
|
Key={
|
||||||
|
'forum_name': { 'S': 'LOLCat Forum'},
|
||||||
|
},
|
||||||
|
UpdateExpression='set Body=:Body',
|
||||||
|
ExpressionAttributeValues={
|
||||||
|
':Body': {'S': ''}
|
||||||
|
})
|
||||||
|
|
||||||
|
ex.exception.response['Error']['Code'].should.equal('ValidationException')
|
||||||
|
ex.exception.response['ResponseMetadata']['HTTPStatusCode'].should.equal(400)
|
||||||
|
ex.exception.response['Error']['Message'].should.equal(
|
||||||
|
'One or more parameter values were invalid: An AttributeValue may not contain an empty string'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@requires_boto_gte("2.9")
|
@requires_boto_gte("2.9")
|
||||||
@mock_dynamodb2
|
@mock_dynamodb2
|
||||||
def test_query_invalid_table():
|
def test_query_invalid_table():
|
||||||
|
Loading…
Reference in New Issue
Block a user