Merge pull request #2850 from dreamorosi/bugfix/dynamodb-put-item-validation-exception

DynamoDB - fix put_item ValidationException
This commit is contained in:
Bert Blommers 2020-03-29 09:42:54 +01:00 committed by GitHub
commit acfdfb7d7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 1 deletions

View File

@ -800,13 +800,19 @@ class Table(BaseModel):
overwrite=False, overwrite=False,
): ):
if self.hash_key_attr not in item_attrs.keys(): if self.hash_key_attr not in item_attrs.keys():
raise ValueError( raise KeyError(
"One or more parameter values were invalid: Missing the key " "One or more parameter values were invalid: Missing the key "
+ self.hash_key_attr + self.hash_key_attr
+ " in the item" + " in the item"
) )
hash_value = DynamoType(item_attrs.get(self.hash_key_attr)) hash_value = DynamoType(item_attrs.get(self.hash_key_attr))
if self.has_range_key: if self.has_range_key:
if self.range_key_attr not in item_attrs.keys():
raise KeyError(
"One or more parameter values were invalid: Missing the key "
+ self.range_key_attr
+ " in the item"
)
range_value = DynamoType(item_attrs.get(self.range_key_attr)) range_value = DynamoType(item_attrs.get(self.range_key_attr))
else: else:
range_value = None range_value = None

View File

@ -299,6 +299,9 @@ class DynamoHandler(BaseResponse):
except ItemSizeTooLarge: except ItemSizeTooLarge:
er = "com.amazonaws.dynamodb.v20111205#ValidationException" er = "com.amazonaws.dynamodb.v20111205#ValidationException"
return self.error(er, ItemSizeTooLarge.message) return self.error(er, ItemSizeTooLarge.message)
except KeyError as ke:
er = "com.amazonaws.dynamodb.v20111205#ValidationException"
return self.error(er, ke.args[0])
except ValueError as ve: except ValueError as ve:
er = "com.amazonaws.dynamodb.v20111205#ConditionalCheckFailedException" er = "com.amazonaws.dynamodb.v20111205#ConditionalCheckFailedException"
return self.error(er, str(ve)) return self.error(er, str(ve))

View File

@ -1345,6 +1345,25 @@ def test_get_item_returns_consumed_capacity():
assert "TableName" in response["ConsumedCapacity"] assert "TableName" in response["ConsumedCapacity"]
@mock_dynamodb2
def test_put_empty_item():
dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
dynamodb.create_table(
AttributeDefinitions=[{"AttributeName": "structure_id", "AttributeType": "S"},],
TableName="test",
KeySchema=[{"AttributeName": "structure_id", "KeyType": "HASH"},],
ProvisionedThroughput={"ReadCapacityUnits": 123, "WriteCapacityUnits": 123},
)
table = dynamodb.Table("test")
with assert_raises(ClientError) as ex:
table.put_item(Item={})
ex.exception.response["Error"]["Message"].should.equal(
"One or more parameter values were invalid: Missing the key structure_id in the item"
)
ex.exception.response["Error"]["Code"].should.equal("ValidationException")
@mock_dynamodb2 @mock_dynamodb2
def test_put_item_nonexisting_hash_key(): def test_put_item_nonexisting_hash_key():
dynamodb = boto3.resource("dynamodb", region_name="us-east-1") dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
@ -1361,6 +1380,32 @@ def test_put_item_nonexisting_hash_key():
ex.exception.response["Error"]["Message"].should.equal( ex.exception.response["Error"]["Message"].should.equal(
"One or more parameter values were invalid: Missing the key structure_id in the item" "One or more parameter values were invalid: Missing the key structure_id in the item"
) )
ex.exception.response["Error"]["Code"].should.equal("ValidationException")
@mock_dynamodb2
def test_put_item_nonexisting_range_key():
dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
dynamodb.create_table(
AttributeDefinitions=[
{"AttributeName": "structure_id", "AttributeType": "S"},
{"AttributeName": "added_at", "AttributeType": "N"},
],
TableName="test",
KeySchema=[
{"AttributeName": "structure_id", "KeyType": "HASH"},
{"AttributeName": "added_at", "KeyType": "RANGE"},
],
ProvisionedThroughput={"ReadCapacityUnits": 123, "WriteCapacityUnits": 123},
)
table = dynamodb.Table("test")
with assert_raises(ClientError) as ex:
table.put_item(Item={"structure_id": "abcdef"})
ex.exception.response["Error"]["Message"].should.equal(
"One or more parameter values were invalid: Missing the key added_at in the item"
)
ex.exception.response["Error"]["Code"].should.equal("ValidationException")
def test_filter_expression(): def test_filter_expression():