Skip to content

Commit

Permalink
DynamoDB: update_item() can now return list of binaries (#8401)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers authored Dec 15, 2024
1 parent a7c3613 commit ea13ff2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions moto/dynamodb/models/utilities.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import json
import re
from typing import Any, Dict, List, Optional
Expand All @@ -7,6 +8,8 @@ class DynamoJsonEncoder(json.JSONEncoder):
def default(self, o: Any) -> Any:
if hasattr(o, "to_json"):
return o.to_json()
elif isinstance(o, bytes):
return base64.b64encode(o).decode("utf-8")


def dynamo_json_dump(dynamo_object: Any) -> str:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_dynamodb/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4952,3 +4952,25 @@ def test_query_with_gsi_reverse_paginated():
{"pri": {"S": "pri_10"}, "alt": {"S": "alt_2"}},
]
assert "LastEvaluatedKey" not in p2


@pytest.mark.aws_verified
@dynamodb_aws_verified()
def test_update_item_with_list_of_bytes(table_name=None):
dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
table = dynamodb.Table(table_name)

b1 = b"\n\x014\x18\xc3\xb0\xf8\xba\x06"
b2 = b"\n\x012\x18\xc3\xb0\xf8\xba\x06"

update = table.update_item(
Key={"pk": "clientA"},
UpdateExpression="SET #items = :new_items",
ExpressionAttributeValues={":new_items": [b1, b2]},
ExpressionAttributeNames={"#items": "items"},
ReturnValues="UPDATED_NEW",
)
assert update["Attributes"]["items"] == [Binary(b1), Binary(b2)]

get = table.get_item(Key={"pk": "clientA"})
assert get["Item"] == {"pk": "clientA", "items": [Binary(b1), Binary(b2)]}

0 comments on commit ea13ff2

Please sign in to comment.