diff --git a/aws_lambda_powertools/utilities/data_classes/dynamo_db_stream_event.py b/aws_lambda_powertools/utilities/data_classes/dynamo_db_stream_event.py index 0fedf55dc2..17150a147d 100644 --- a/aws_lambda_powertools/utilities/data_classes/dynamo_db_stream_event.py +++ b/aws_lambda_powertools/utilities/data_classes/dynamo_db_stream_event.py @@ -73,7 +73,12 @@ def _deserialize_bool(self, value: bool) -> bool: return value def _deserialize_n(self, value: str) -> Decimal: + # value is None or "."? It's zero + # then return early value = value.lstrip("0") + if not value or value == ".": + return DYNAMODB_CONTEXT.create_decimal(0) + if len(value) > 38: # See: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes.Number # Calculate the number of trailing zeros after the 38th character diff --git a/tests/unit/data_classes/required_dependencies/test_dynamo_db_stream_event.py b/tests/unit/data_classes/required_dependencies/test_dynamo_db_stream_event.py index 464d9a0eba..d893e08840 100644 --- a/tests/unit/data_classes/required_dependencies/test_dynamo_db_stream_event.py +++ b/tests/unit/data_classes/required_dependencies/test_dynamo_db_stream_event.py @@ -75,6 +75,22 @@ def test_dynamodb_stream_record_deserialization_large_int_without_trailing_zeros } +def test_dynamodb_stream_record_deserialization_zero_value(): + + data = { + "Keys": {"key1": {"attr1": "value1"}}, + "NewImage": { + "Name": {"S": "Joe"}, + "Age": {"N": "0"}, + }, + } + record = StreamRecord(data) + assert record.new_image == { + "Name": "Joe", + "Age": DECIMAL_CONTEXT.create_decimal("0"), + } + + def test_dynamodb_stream_record_deserialization(): byte_list = [s.encode("utf-8") for s in ["item1", "item2"]] decimal_context = Context(