From ea13ff2c4aea37623c5ca9c27a0bd1465afce5bd Mon Sep 17 00:00:00 2001 From: Bert Blommers Date: Sun, 15 Dec 2024 17:06:33 -0100 Subject: [PATCH] DynamoDB: update_item() can now return list of binaries (#8401) --- moto/dynamodb/models/utilities.py | 3 +++ tests/test_dynamodb/test_dynamodb.py | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/moto/dynamodb/models/utilities.py b/moto/dynamodb/models/utilities.py index 6d4636e787a4..52b97efc4be5 100644 --- a/moto/dynamodb/models/utilities.py +++ b/moto/dynamodb/models/utilities.py @@ -1,3 +1,4 @@ +import base64 import json import re from typing import Any, Dict, List, Optional @@ -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: diff --git a/tests/test_dynamodb/test_dynamodb.py b/tests/test_dynamodb/test_dynamodb.py index f8b930289ec4..1905c4e8c4fb 100644 --- a/tests/test_dynamodb/test_dynamodb.py +++ b/tests/test_dynamodb/test_dynamodb.py @@ -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)]}