Skip to content

Commit

Permalink
DynamoDB - Return binary data in right format (#6828)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers committed Sep 18, 2023
1 parent ab8bf21 commit 643cf7c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
2 changes: 1 addition & 1 deletion moto/dynamodb/models/dynamo_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def size(self) -> int:

def to_json(self) -> Dict[str, Any]:
# Returns a regular JSON object where the value can still be/contain a DynamoType
if self.is_binary():
if self.is_binary() and isinstance(self.value, bytes):
# Binary data cannot be represented in JSON
# AWS returns a base64-encoded value - the SDK's then convert that back
return {self.type: base64.b64encode(self.value).decode("utf-8")}
Expand Down
12 changes: 10 additions & 2 deletions tests/test_dynamodb/test_dynamodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5745,11 +5745,19 @@ def test_projection_expression_with_binary_attr():
)
table = dynamo_resource.Table("test")
table.put_item(Item={"pk": "pk", "sk": "sk", "key": b"value\xbf"})
assert table.get_item(

item = table.get_item(
Key={"pk": "pk", "sk": "sk"},
ExpressionAttributeNames={"#key": "key"},
ProjectionExpression="#key",
)["Item"] == {"key": Binary(b"value\xbf")}
)["Item"]
assert item == {"key": Binary(b"value\xbf")}

item = table.scan()["Items"][0]
assert item["key"] == Binary(b"value\xbf")

item = table.query(KeyConditionExpression=Key("pk").eq("pk"))["Items"][0]
assert item["key"] == Binary(b"value\xbf")


@mock_dynamodb
Expand Down
29 changes: 11 additions & 18 deletions tests/test_dynamodb/test_dynamodb_batch_get_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def _create_user_table():
ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
)
client.put_item(
TableName="users", Item={"username": {"S": "user1"}, "foo": {"S": "bar"}}
TableName="users", Item={"username": {"S": "user1"}, "binaryfoo": {"B": b"bar"}}
)
client.put_item(
TableName="users", Item={"username": {"S": "user2"}, "foo": {"S": "bar"}}
Expand Down Expand Up @@ -42,11 +42,9 @@ def test_batch_items_returns_all():
}
)["Responses"]["users"]
assert len(returned_items) == 3
assert [item["username"]["S"] for item in returned_items] == [
"user1",
"user2",
"user3",
]
assert {"username": {"S": "user1"}, "binaryfoo": {"B": b"bar"}} in returned_items
assert {"username": {"S": "user2"}, "foo": {"S": "bar"}} in returned_items
assert {"username": {"S": "user3"}, "foo": {"S": "bar"}} in returned_items


@mock_dynamodb
Expand Down Expand Up @@ -137,12 +135,10 @@ def test_batch_items_with_basic_projection_expression():
}
)["Responses"]["users"]

assert [item["username"]["S"] for item in returned_items] == [
"user1",
"user2",
"user3",
]
assert [item["foo"]["S"] for item in returned_items] == ["bar", "bar", "bar"]
assert len(returned_items) == 3
assert {"username": {"S": "user1"}, "binaryfoo": {"B": b"bar"}} in returned_items
assert {"username": {"S": "user2"}, "foo": {"S": "bar"}} in returned_items
assert {"username": {"S": "user3"}, "foo": {"S": "bar"}} in returned_items


@mock_dynamodb
Expand All @@ -165,12 +161,9 @@ def test_batch_items_with_basic_projection_expression_and_attr_expression_names(
)["Responses"]["users"]

assert len(returned_items) == 3
assert [item["username"]["S"] for item in returned_items] == [
"user1",
"user2",
"user3",
]
assert [item.get("foo") for item in returned_items] == [None, None, None]
assert {"username": {"S": "user1"}} in returned_items
assert {"username": {"S": "user2"}} in returned_items
assert {"username": {"S": "user3"}} in returned_items


@mock_dynamodb
Expand Down

0 comments on commit 643cf7c

Please sign in to comment.