Skip to content

Commit cb8d6a0

Browse files
committed
fix: ensures parser can take json strings as input
1 parent ba7cd29 commit cb8d6a0

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

aws_lambda_powertools/utilities/parser/parser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ def handler(event: Order, context: LambdaContext):
149149

150150
try:
151151
logger.debug("Parsing and validating event model; no envelope used")
152+
if isinstance(event, str):
153+
return model.parse_raw(event)
154+
152155
return model.parse_obj(event)
153156
except (ValidationError, TypeError) as e:
154157
raise ModelValidationError("Input event does not conform with model") from e

docs/content/utilities/parser.mdx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ These are simply Python classes that inherit from BaseModel. **Parser** enforces
5959

6060
## Parsing events
6161

62-
You can parse inbound events using **event_parser** decorator, or the standalone `parse` function.
62+
You can parse inbound events using **event_parser** decorator, or the standalone `parse` function. Both are also able to parse either dictionary or JSON string as an input.
6363

6464
### event_parser decorator
6565

@@ -70,6 +70,20 @@ Use the decorator for fail fast scenarios where you want your Lambda function to
7070
```python=:title=event_parser_decorator.py
7171
from aws_lambda_powertools.utilities.parser import parse, ModelValidationError
7272
from aws_lambda_powertools.utilities.typing import LambdaContext
73+
import json
74+
75+
# Raw event for the Order model we've defined earlier
76+
payload = {
77+
"id": 10876546789,
78+
"description": "My order",
79+
"items": [
80+
{
81+
"id": 1015938732,
82+
"quantity": 1,
83+
"description": "item xpto"
84+
}
85+
]
86+
}
7387

7488
@event_parser(model=Order) # highlight-line
7589
def handler(event: Order, context: LambdaContext):
@@ -79,6 +93,9 @@ def handler(event: Order, context: LambdaContext):
7993

8094
order_items = [items for item in event.items]
8195
...
96+
97+
handler(event=payload, context=LambdaContext())
98+
handler(event=json.dumps(payload), context=LambdaContext()) # also works if event is a JSON string
8299
```
83100

84101
### parse function
@@ -113,6 +130,10 @@ def my_function():
113130
}
114131
```
115132

133+
### Error handling
134+
135+
**TBW**
136+
116137
## Built-in envelopes
117138

118139
**TBW**

tests/functional/parser/test_parser.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Dict
1+
import json
2+
from typing import Dict, Union
23

34
import pytest
45

@@ -55,3 +56,13 @@ def handle_no_envelope(event: Dict, _: LambdaContext):
5556

5657
with pytest.raises(exceptions.InvalidModelTypeError):
5758
handle_no_envelope(event=dummy_event, context=LambdaContext())
59+
60+
61+
def test_parser_event_as_json_string(dummy_event, dummy_schema):
62+
dummy_event = json.dumps(dummy_event["payload"])
63+
64+
@event_parser(model=dummy_schema)
65+
def handle_no_envelope(event: Union[Dict, str], _: LambdaContext):
66+
return event
67+
68+
handle_no_envelope(dummy_event, LambdaContext())

0 commit comments

Comments
 (0)