Skip to content

fix(idempotency): pass by value on idem key to guard inadvertent mutations #1090

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion aws_lambda_powertools/utilities/idempotency/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from typing import Any, Callable, Dict, Optional, Tuple
from copy import deepcopy

from aws_lambda_powertools.utilities.idempotency.config import IdempotencyConfig
from aws_lambda_powertools.utilities.idempotency.exceptions import (
Expand Down Expand Up @@ -69,7 +70,7 @@ def __init__(
Function keyword arguments
"""
self.function = function
self.data = _prepare_data(function_payload)
self.data = deepcopy(_prepare_data(function_payload))
self.fn_args = function_args
self.fn_kwargs = function_kwargs

Expand Down
34 changes: 34 additions & 0 deletions tests/functional/idempotency/test_idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,40 @@ def lambda_handler(event, context):
stubber.assert_no_pending_responses()
stubber.deactivate()

@pytest.mark.parametrize("idempotency_config", [{"use_local_cache": False}, {"use_local_cache": True}], indirect=True)
def test_idempotent_lambda_first_execution_event_mutation(
idempotency_config: IdempotencyConfig,
persistence_store: DynamoDBPersistenceLayer,
lambda_apigw_event,
expected_params_update_item,
expected_params_put_item,
lambda_response,
serialized_lambda_response,
deserialized_lambda_response,
hashed_idempotency_key,
lambda_context,
):
"""
Test idempotent decorator where lambda_handler mutates the event
"""

stubber = stub.Stubber(persistence_store.table.meta.client)
ddb_response = {}

stubber.add_response("put_item", ddb_response, expected_params_put_item)
stubber.add_response("update_item", ddb_response, expected_params_update_item)
stubber.activate()

@idempotent(config=idempotency_config, persistence_store=persistence_store)
def lambda_handler(event, context):
event.popitem()
return lambda_response

lambda_handler(lambda_apigw_event, lambda_context)

stubber.assert_no_pending_responses()
stubber.deactivate()


@pytest.mark.parametrize("idempotency_config", [{"use_local_cache": False}, {"use_local_cache": True}], indirect=True)
def test_idempotent_lambda_expired(
Expand Down