Skip to content

Commit 8074a54

Browse files
fix(data_classes): return None for the SQS attribute value an attribute does not carry (#8713)
Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
1 parent d28d84a commit 8074a54

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

‎aws_lambda_powertools/utilities/data_classes/sqs_event.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ class SQSMessageAttribute(DictWrapper):
7474

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

8080
@property
8181
def binary_value(self) -> str | None:
8282
"""Binary type attributes can store any binary data, such as compressed data, encrypted data, or images.
8383
84-
Base64-encoded binary data object"""
85-
return self["binaryValue"]
84+
Base64-encoded binary data object. None for a String or Number attribute."""
85+
return self.get("binaryValue")
8686

8787
@property
8888
def data_type(self) -> str:

‎tests/unit/data_classes/required_dependencies/test_sqs_event.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,22 @@ def test_sqs_event_typing():
169169

170170
# This assertion compares the return from .items() to the return of __getitem__
171171
assert list(attributes.items())[0][1] == attributes["key"]
172+
173+
174+
def test_sqs_message_attribute_with_only_one_value():
175+
# GIVEN message attributes as Lambda delivers them: a String attribute carries no binaryValue
176+
# and a Binary attribute carries no stringValue
177+
raw_event = load_event("sqsEvent.json")
178+
raw_event["Records"][0]["messageAttributes"] = {
179+
"text": {"stringValue": "myValue", "stringListValues": [], "binaryListValues": [], "dataType": "String"},
180+
"blob": {"binaryValue": "YmFzZTY0", "stringListValues": [], "binaryListValues": [], "dataType": "Binary"},
181+
}
182+
183+
# WHEN reading the attributes
184+
attributes = next(SQSEvent(raw_event).records).message_attributes
185+
186+
# THEN the value a type doesn't carry is None
187+
assert attributes["text"].string_value == "myValue"
188+
assert attributes["text"].binary_value is None
189+
assert attributes["blob"].binary_value == "YmFzZTY0"
190+
assert attributes["blob"].string_value is None

0 commit comments

Comments
 (0)