Skip to content

Commit

Permalink
fix(event_source): fix regression when working with zero numbers in D…
Browse files Browse the repository at this point in the history
…ynamoDBStreamEvent (#4932)

Fix regression when working with ZERO
  • Loading branch information
leandrodamascena authored Aug 12, 2024
1 parent 599dceb commit 2b6f605
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 2b6f605

Please sign in to comment.