Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inject_condition_expressions transform for nested inputs such as … #4054

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions boto3/dynamodb/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,11 @@ def __call__(self, value):
built_expression = self._condition_builder.build_expression(
value, is_key_condition=self._is_key_condition
)

self._placeholder_names.update(
built_expression.attribute_name_placeholders
)
self._placeholder_values.update(
built_expression.attribute_value_placeholders
)

return built_expression.condition_expression
return {
"PARAM_NAME_PLACEHOLDER": built_expression.condition_expression,
"ExpressionAttributeNames": built_expression.attribute_name_placeholders,
"ExpressionAttributeValues": built_expression.attribute_value_placeholders
}
# Use the user provided value if it is not a ConditonBase object.
return value

Expand Down Expand Up @@ -302,12 +298,24 @@ def _transform_structure(
):
if not isinstance(params, collections_abc.Mapping):
return
for param in params:
for param in copy.deepcopy(params):
if param in model.members:
member_model = model.members[param]
member_shape = member_model.name
if member_shape == target_shape:
params[param] = transformation(params[param])
new_value = transformation(params[param])
if isinstance(new_value, dict):
if "PARAM_NAME_PLACEHOLDER" in new_value:
new_value[param] = new_value.pop("PARAM_NAME_PLACEHOLDER")
for key, value in new_value.items():
if isinstance(value, dict) and key in params and isinstance(params[key], dict):
params[key].update(value)
else:
params[key] = value
else:
params[param] = new_value
else:
params[param] = new_value
else:
self._transform_parameters(
member_model,
Expand Down
49 changes: 48 additions & 1 deletion tests/unit/dynamodb/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,33 @@ def setup_models(self):
'name': 'SampleOperation',
'input': {'shape': 'SampleOperationInputOutput'},
'output': {'shape': 'SampleOperationInputOutput'},
}
},
'SampleListOperation': {
'name': 'SampleListOperation',
'input': {'shape': 'SampleOperationListInputOutput'},
'output': {'shape': 'SampleOperationListInputOutput'},
},
'SampleOperationStructList': {
'name': 'SampleOperationStructListInputOutput',
'input': {'shape': 'SampleOperationStructListInputOutput'},
'output': {'shape': 'SampleOperationStructListInputOutput'},
},
},
'shapes': {
'SampleOperationInputOutput': {
'type': 'structure',
'members': {},
},
'SampleOperationListInputOutput': {
'type': 'list',
'member': {'shape': 'SampleOperationInputOutput'},
},
'SampleOperationStructListInputOutput': {
'type': 'structure',
'members': {
'InputList': {'shape': 'SampleOperationListInputOutput'},
},
},
'String': {'type': 'string'},
},
}
Expand Down Expand Up @@ -571,6 +591,33 @@ def test_key_and_attr_conditon_expression_with_placeholders(self):
},
}

def test_key_and_attr_condition_expression_nested_inside_list(self):
self.operation_model = OperationModel(self.json_model['operations']["SampleOperationStructList"], self.service_model)
params = {"InputList":[{
'KeyCondition': Key('foo').eq('bar'),
'AttrCondition': Attr('biz').eq('baz'),
'ExpressionAttributeNames': {'#a': 'b'},
'ExpressionAttributeValues': {':c': 'd'},
}]}
self.injector.inject_condition_expressions(
params, self.operation_model
)
assert params == {"InputList":[{
'KeyCondition': '#n1 = :v1',
'AttrCondition': '#n0 = :v0',
'ExpressionAttributeNames': {
'#n0': 'biz',
'#n1': 'foo',
'#a': 'b'
},
'ExpressionAttributeValues': {
':v0': 'baz',
':v1': 'bar',
':c': 'd'
},
}]}



class TestCopyDynamoDBParams(unittest.TestCase):
def test_copy_dynamodb_params(self):
Expand Down