Skip to content

Commit d07a9c2

Browse files
fix(parser): accept SES events without common headers or invocation type (#8768)
Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
1 parent 1163301 commit d07a9c2

4 files changed

Lines changed: 85 additions & 19 deletions

File tree

‎aws_lambda_powertools/utilities/data_classes/ses_event.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,35 @@ def value(self) -> str:
2020

2121
class SESMailCommonHeaders(DictWrapper):
2222
@property
23-
def return_path(self) -> str:
23+
def return_path(self) -> str | None:
2424
"""The values in the Return-Path header of the email."""
25-
return self["returnPath"]
25+
return self.get("returnPath")
2626

2727
@property
2828
def get_from(self) -> list[str]:
2929
"""The values in the From header of the email."""
3030
# Note: this name conflicts with existing python builtins
31-
return self["from"]
31+
return self.get("from") or []
3232

3333
@property
34-
def date(self) -> str:
34+
def date(self) -> str | None:
3535
"""The date and time when Amazon SES received the message."""
36-
return self["date"]
36+
return self.get("date")
3737

3838
@property
3939
def to(self) -> list[str]:
4040
"""The values in the To header of the email."""
41-
return self["to"]
41+
return self.get("to") or []
4242

4343
@property
44-
def message_id(self) -> str:
44+
def message_id(self) -> str | None:
4545
"""The ID of the original message."""
46-
return str(self["messageId"])
46+
return self.get("messageId")
4747

4848
@property
49-
def subject(self) -> str:
49+
def subject(self) -> str | None:
5050
"""The value of the Subject header for the email."""
51-
return str(self["subject"])
51+
return self.get("subject")
5252

5353
@property
5454
def cc(self) -> list[str]:
@@ -148,10 +148,10 @@ def function_arn(self) -> str:
148148
return self["functionArn"]
149149

150150
@property
151-
def invocation_type(self) -> str:
151+
def invocation_type(self) -> str | None:
152152
"""String that contains the invocation type of the Lambda function. Possible values are RequestResponse
153153
and Event. Present only for the Lambda action type."""
154-
return self["invocationType"]
154+
return self.get("invocationType")
155155

156156

157157
class SESReceipt(DictWrapper):

‎aws_lambda_powertools/utilities/parser/models/ses.py‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SesReceiptVerdict(BaseModel):
1111

1212
class SesReceiptAction(BaseModel):
1313
type: Literal["Lambda"] # noqa A003,VNE003
14-
invocationType: Literal["Event"]
14+
invocationType: Literal["Event", "RequestResponse"] | None = None
1515
functionArn: str
1616

1717

@@ -32,8 +32,9 @@ class SesMailHeaders(BaseModel):
3232

3333

3434
class SesMailCommonHeaders(BaseModel):
35-
header_from: list[str] = Field(..., alias="from")
36-
to: list[str]
35+
# SES sends each common header only when the email has that header
36+
header_from: list[str] | None = Field(None, alias="from")
37+
to: list[str] | None = None
3738
cc: list[str] | None = None
3839
bcc: list[str] | None = None
3940
sender: list[str] | None = None
@@ -42,10 +43,10 @@ class SesMailCommonHeaders(BaseModel):
4243
validation_alias=AliasChoices("replyTo", "reply-to", "reply_to"),
4344
serialization_alias="replyTo",
4445
)
45-
returnPath: str
46-
messageId: str
47-
date: str
48-
subject: str
46+
returnPath: str | None = None
47+
messageId: str | None = None
48+
date: str | None = None
49+
subject: str | None = None
4950

5051

5152
class SesMail(BaseModel):

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,24 @@ def test_ses_trigger_event():
5555
assert parsed_event.record.raw_event == raw_event["Records"][0]
5656
assert parsed_event.mail.raw_event == raw_event["Records"][0]["ses"]["mail"]
5757
assert parsed_event.receipt.raw_event == raw_event["Records"][0]["ses"]["receipt"]
58+
59+
60+
def test_ses_trigger_event_without_common_headers_or_invocation_type():
61+
# GIVEN an SES event for an email without any common headers and a Lambda action without an invocation type
62+
raw_event = load_event("sesEvent.json")
63+
raw_event["Records"][0]["ses"]["mail"]["commonHeaders"] = {}
64+
del raw_event["Records"][0]["ses"]["receipt"]["action"]["invocationType"]
65+
66+
# WHEN reading the common headers and the action
67+
parsed_event = SESEvent(raw_event)
68+
common_headers = parsed_event.mail.common_headers
69+
action = parsed_event.receipt.action
70+
71+
# THEN the missing headers are empty instead of raising KeyError
72+
assert common_headers.return_path is None
73+
assert common_headers.get_from == []
74+
assert common_headers.date is None
75+
assert common_headers.to == []
76+
assert common_headers.message_id is None
77+
assert common_headers.subject is None
78+
assert action.invocation_type is None

‎tests/unit/parser/_pydantic/test_ses.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from aws_lambda_powertools.utilities.parser.models import SesModel, SesRecordModel
24
from tests.functional.utils import load_event
35

@@ -117,3 +119,45 @@ def test_ses_trigger_event_with_optional_common_headers():
117119
assert common_headers.bcc == ["bcc@example.com"]
118120
assert common_headers.sender == ["sender@example.com"]
119121
assert common_headers.reply_to == ["reply@example.com"]
122+
123+
124+
@pytest.mark.parametrize("invocation_type", ["Event", "RequestResponse"])
125+
def test_ses_trigger_event_with_invocation_type(invocation_type):
126+
# GIVEN an SES event from a Lambda action with either documented invocation type
127+
raw_event = load_event("sesEvent.json")
128+
raw_event["Records"][0]["ses"]["receipt"]["action"]["invocationType"] = invocation_type
129+
130+
# WHEN parsing the event
131+
action = SesModel(**raw_event).Records[0].ses.receipt.action
132+
133+
# THEN the invocation type is kept
134+
assert action.invocationType == invocation_type
135+
136+
137+
def test_ses_trigger_event_without_invocation_type():
138+
# GIVEN an SES event whose Lambda action has no invocation type
139+
raw_event = load_event("sesEvent.json")
140+
del raw_event["Records"][0]["ses"]["receipt"]["action"]["invocationType"]
141+
142+
# WHEN parsing the event
143+
action = SesModel(**raw_event).Records[0].ses.receipt.action
144+
145+
# THEN the invocation type is None
146+
assert action.invocationType is None
147+
148+
149+
def test_ses_trigger_event_without_common_headers():
150+
# GIVEN an SES event for an email without any common headers (SES only sends the ones the email has)
151+
raw_event = load_event("sesEvent.json")
152+
raw_event["Records"][0]["ses"]["mail"]["commonHeaders"] = {}
153+
154+
# WHEN parsing the event
155+
common_headers = SesModel(**raw_event).Records[0].ses.mail.commonHeaders
156+
157+
# THEN every common header is None
158+
assert common_headers.header_from is None
159+
assert common_headers.to is None
160+
assert common_headers.returnPath is None
161+
assert common_headers.messageId is None
162+
assert common_headers.date is None
163+
assert common_headers.subject is None

0 commit comments

Comments
 (0)