DynamoDB - Return binary data in right format (#6828)

This commit is contained in:
Bert Blommers 2023-09-18 21:22:03 +00:00 committed by GitHub
parent ab8bf21729
commit 643cf7c55e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 21 deletions

View File

@ -196,7 +196,7 @@ class DynamoType(object):
def to_json(self) -> Dict[str, Any]: def to_json(self) -> Dict[str, Any]:
# Returns a regular JSON object where the value can still be/contain a DynamoType # 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 # Binary data cannot be represented in JSON
# AWS returns a base64-encoded value - the SDK's then convert that back # AWS returns a base64-encoded value - the SDK's then convert that back
return {self.type: base64.b64encode(self.value).decode("utf-8")} return {self.type: base64.b64encode(self.value).decode("utf-8")}

View File

@ -5745,11 +5745,19 @@ def test_projection_expression_with_binary_attr():
) )
table = dynamo_resource.Table("test") table = dynamo_resource.Table("test")
table.put_item(Item={"pk": "pk", "sk": "sk", "key": b"value\xbf"}) 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"}, Key={"pk": "pk", "sk": "sk"},
ExpressionAttributeNames={"#key": "key"}, ExpressionAttributeNames={"#key": "key"},
ProjectionExpression="#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 @mock_dynamodb

View File

@ -14,7 +14,7 @@ def _create_user_table():
ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5}, ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
) )
client.put_item( 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( client.put_item(
TableName="users", Item={"username": {"S": "user2"}, "foo": {"S": "bar"}} TableName="users", Item={"username": {"S": "user2"}, "foo": {"S": "bar"}}
@ -42,11 +42,9 @@ def test_batch_items_returns_all():
} }
)["Responses"]["users"] )["Responses"]["users"]
assert len(returned_items) == 3 assert len(returned_items) == 3
assert [item["username"]["S"] for item in returned_items] == [ assert {"username": {"S": "user1"}, "binaryfoo": {"B": b"bar"}} in returned_items
"user1", assert {"username": {"S": "user2"}, "foo": {"S": "bar"}} in returned_items
"user2", assert {"username": {"S": "user3"}, "foo": {"S": "bar"}} in returned_items
"user3",
]
@mock_dynamodb @mock_dynamodb
@ -137,12 +135,10 @@ def test_batch_items_with_basic_projection_expression():
} }
)["Responses"]["users"] )["Responses"]["users"]
assert [item["username"]["S"] for item in returned_items] == [ assert len(returned_items) == 3
"user1", assert {"username": {"S": "user1"}, "binaryfoo": {"B": b"bar"}} in returned_items
"user2", assert {"username": {"S": "user2"}, "foo": {"S": "bar"}} in returned_items
"user3", assert {"username": {"S": "user3"}, "foo": {"S": "bar"}} in returned_items
]
assert [item["foo"]["S"] for item in returned_items] == ["bar", "bar", "bar"]
@mock_dynamodb @mock_dynamodb
@ -165,12 +161,9 @@ def test_batch_items_with_basic_projection_expression_and_attr_expression_names(
)["Responses"]["users"] )["Responses"]["users"]
assert len(returned_items) == 3 assert len(returned_items) == 3
assert [item["username"]["S"] for item in returned_items] == [ assert {"username": {"S": "user1"}} in returned_items
"user1", assert {"username": {"S": "user2"}} in returned_items
"user2", assert {"username": {"S": "user3"}} in returned_items
"user3",
]
assert [item.get("foo") for item in returned_items] == [None, None, None]
@mock_dynamodb @mock_dynamodb