Skip to content

Commit 083460d

Browse files
fix(parser): fall back to Python validation for non-JSON string payloads (#8699)
* fix(parser): fall back to Python validation for non-JSON string payloads * refactor(parser): move the string payload fallback into a helper to keep the module complexity rank --------- Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
1 parent 8b5ec90 commit 083460d

2 files changed

Lines changed: 84 additions & 11 deletions

File tree

‎aws_lambda_powertools/utilities/parser/functions.py‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
from typing import TYPE_CHECKING, Any
66

7-
from pydantic import IPvAnyNetwork, TypeAdapter
7+
from pydantic import IPvAnyNetwork, TypeAdapter, ValidationError
88

99
from aws_lambda_powertools.shared.cache_dict import LRUDict
1010

@@ -49,6 +49,28 @@ def _retrieve_or_set_model_from_cache(model: type[T]) -> TypeAdapter[T]:
4949
return CACHE_TYPE_ADAPTER[id_model]
5050

5151

52+
def _parse_string_payload(data: str, adapter: TypeAdapter[T]):
53+
"""Validate a string payload as JSON first, then as a plain Python value.
54+
55+
Falls back to Python validation when Pydantic cannot validate the type from JSON
56+
(see https://github.com/aws-powertools/powertools-lambda-python/issues/5303) and when
57+
the string is not JSON at all, e.g. the plain-text Lambda runtime lines that CloudWatch Logs
58+
subscriptions forward (see https://github.com/aws-powertools/powertools-lambda-python/issues/8695).
59+
If the fallback fails too, the original JSON error is raised so callers see the same message.
60+
"""
61+
try:
62+
return adapter.validate_json(data)
63+
except NotImplementedError:
64+
# See: https://github.com/pydantic/pydantic/issues/8890
65+
logger.debug("Falling back to Python validation due to Pydantic implementation.")
66+
return adapter.validate_python(json.loads(data))
67+
except ValidationError as exc:
68+
try:
69+
return adapter.validate_python(data)
70+
except ValidationError:
71+
raise exc from None
72+
73+
5274
def _parse_and_validate_event(data: dict[str, Any] | Any, adapter: TypeAdapter[T]):
5375
"""
5476
Parse and validate the event data using the provided adapter.
@@ -70,16 +92,7 @@ def _parse_and_validate_event(data: dict[str, Any] | Any, adapter: TypeAdapter[T
7092

7193
if isinstance(data, str):
7294
logger.debug("Parsing event as string")
73-
try:
74-
return adapter.validate_json(data)
75-
except NotImplementedError:
76-
# See: https://github.com/aws-powertools/powertools-lambda-python/issues/5303
77-
# See: https://github.com/pydantic/pydantic/issues/8890
78-
logger.debug(
79-
"Falling back to Python validation due to Pydantic implementation."
80-
"See issue: https://github.com/aws-powertools/powertools-lambda-python/issues/5303",
81-
)
82-
data = json.loads(data)
95+
return _parse_string_payload(data, adapter)
8396

8497
return adapter.validate_python(data)
8598

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,63 @@ def test_handle_invalid_event_with_envelope():
126126
empty_dict = {}
127127
with pytest.raises(ValidationError):
128128
CloudWatchLogsModel(**empty_dict)
129+
130+
131+
def _make_cloudwatch_event(messages: list[str]) -> dict:
132+
inner_event_dict = {
133+
"messageType": "DATA_MESSAGE",
134+
"owner": "123456789012",
135+
"logGroup": "/aws/lambda/my-function",
136+
"logStream": "2024/10/21/[$LATEST]abcdef1234567890",
137+
"subscriptionFilters": ["my-filter"],
138+
"logEvents": [
139+
{"id": f"event-{i}", "timestamp": 1729520895158 + i, "message": message}
140+
for i, message in enumerate(messages)
141+
],
142+
}
143+
compressed = zlib.compress(json.dumps(inner_event_dict).encode("utf-8"), -1)
144+
return {"awslogs": {"data": base64.b64encode(compressed)}}
145+
146+
147+
def test_cloudwatch_envelope_handles_non_json_messages():
148+
plain_text_messages = [
149+
"START RequestId: eadc91c8-8273-4a2d-b170-b0a7883b619e Version: $LATEST\n",
150+
"2024-10-21T14:28:15.158Z\teadc91c8-8273-4a2d-b170-b0a7883b619e\tINFO\tHello from handler\n",
151+
"END RequestId: eadc91c8-8273-4a2d-b170-b0a7883b619e\n",
152+
(
153+
"REPORT RequestId: eadc91c8-8273-4a2d-b170-b0a7883b619e\tDuration: 2.07 ms\t"
154+
"Billed Duration: 3 ms\tMemory Size: 128 MB\tMax Memory Used: 67 MB\t\n"
155+
),
156+
]
157+
158+
parsed = parse(
159+
event=_make_cloudwatch_event(plain_text_messages),
160+
model=str,
161+
envelope=envelopes.CloudWatchLogsEnvelope,
162+
)
163+
164+
assert parsed == plain_text_messages
165+
166+
167+
def test_cloudwatch_envelope_handles_mixed_json_and_plain_text_messages():
168+
messages = [
169+
json.dumps({"my_message": "hello", "user": "test"}),
170+
"START RequestId: eadc91c8-8273-4a2d-b170-b0a7883b619e Version: $LATEST\n",
171+
]
172+
173+
parsed = parse(
174+
event=_make_cloudwatch_event(messages),
175+
model=str,
176+
envelope=envelopes.CloudWatchLogsEnvelope,
177+
)
178+
179+
assert parsed == messages
180+
181+
182+
def test_cloudwatch_envelope_plain_text_still_fails_for_object_model():
183+
with pytest.raises(ValidationError):
184+
parse(
185+
event=_make_cloudwatch_event(["START RequestId: eadc91c8-8273-4a2d-b170-b0a7883b619e\n"]),
186+
model=MyCloudWatchBusiness,
187+
envelope=envelopes.CloudWatchLogsEnvelope,
188+
)

0 commit comments

Comments
 (0)