Merge pull request #2491 from bblommers/bugfix/250

DynamoDB - Add validation for Query Key Expression
This commit is contained in:
Mike Grima 2019-10-18 09:52:23 -07:00 committed by GitHub
commit 484da34022
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -457,6 +457,9 @@ class DynamoHandler(BaseResponse):
range_comparison = None
range_values = []
if '=' not in hash_key_expression:
return self.error('com.amazonaws.dynamodb.v20111205#ValidationException',
'Query key condition not supported')
hash_key_value_alias = hash_key_expression.split("=")[1].strip()
# Temporary fix until we get proper KeyConditionExpression function
hash_key = value_alias_map.get(hash_key_value_alias, {'S': hash_key_value_alias})

View File

@ -2705,6 +2705,31 @@ def test_item_size_is_under_400KB():
Item={'id': {'S': 'foo'}, 'itemlist': {'L': [{'M': {'item1': {'S': large_item}}}]}})
@mock_dynamodb2
# https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html#DDB-Query-request-KeyConditionExpression
def test_hash_key_cannot_use_begins_with_operations():
dynamodb = boto3.resource('dynamodb')
table = dynamodb.create_table(
TableName='test-table',
KeySchema=[{'AttributeName': 'key', 'KeyType': 'HASH'}],
AttributeDefinitions=[{'AttributeName': 'key', 'AttributeType': 'S'}],
ProvisionedThroughput={'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1})
items = [{'key': 'prefix-$LATEST', 'value': '$LATEST'},
{'key': 'prefix-DEV', 'value': 'DEV'},
{'key': 'prefix-PROD', 'value': 'PROD'}]
with table.batch_writer() as batch:
for item in items:
batch.put_item(Item=item)
table = dynamodb.Table('test-table')
with assert_raises(ClientError) as ex:
table.query(KeyConditionExpression=Key('key').begins_with('prefix-'))
ex.exception.response['Error']['Code'].should.equal('ValidationException')
ex.exception.response['Error']['Message'].should.equal('Query key condition not supported')
def assert_failure_due_to_item_size(func, **kwargs):
with assert_raises(ClientError) as ex:
func(**kwargs)