Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions aws_lambda_powertools/utilities/data_classes/sqs_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ class SQSMessageAttribute(DictWrapper):

@property
def string_value(self) -> str | None:
"""Strings are Unicode with UTF-8 binary encoding."""
return self["stringValue"]
"""Strings are Unicode with UTF-8 binary encoding. None for a Binary attribute."""
return self.get("stringValue")

@property
def binary_value(self) -> str | None:
"""Binary type attributes can store any binary data, such as compressed data, encrypted data, or images.

Base64-encoded binary data object"""
return self["binaryValue"]
Base64-encoded binary data object. None for a String or Number attribute."""
return self.get("binaryValue")

@property
def data_type(self) -> str:
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/data_classes/required_dependencies/test_sqs_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,22 @@ def test_sqs_event_typing():

# This assertion compares the return from .items() to the return of __getitem__
assert list(attributes.items())[0][1] == attributes["key"]


def test_sqs_message_attribute_with_only_one_value():
# GIVEN message attributes as Lambda delivers them: a String attribute carries no binaryValue
# and a Binary attribute carries no stringValue
raw_event = load_event("sqsEvent.json")
raw_event["Records"][0]["messageAttributes"] = {
"text": {"stringValue": "myValue", "stringListValues": [], "binaryListValues": [], "dataType": "String"},
"blob": {"binaryValue": "YmFzZTY0", "stringListValues": [], "binaryListValues": [], "dataType": "Binary"},
}

# WHEN reading the attributes
attributes = next(SQSEvent(raw_event).records).message_attributes

# THEN the value a type doesn't carry is None
assert attributes["text"].string_value == "myValue"
assert attributes["text"].binary_value is None
assert attributes["blob"].binary_value == "YmFzZTY0"
assert attributes["blob"].string_value is None