Skip to content

Commit 1ebb019

Browse files
docs(parser): add JSON string field extension example (#1526)
Co-authored-by: Heitor Lessa <[email protected]>
1 parent a74fda3 commit 1ebb019

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

docs/utilities/parser.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ Parser comes with the following built-in models:
171171
| **KafkaSelfManagedEventModel** | Lambda Event Source payload for self managed Kafka payload |
172172
| **KafkaMskEventModel** | Lambda Event Source payload for AWS MSK payload |
173173

174-
### extending built-in models
174+
### Extending built-in models
175175

176176
You can extend them to include your own models, and yet have all other known fields parsed along the way.
177177

@@ -236,6 +236,20 @@ for order_item in ret.detail.items:
236236
3. Defined how part of our EventBridge event should look like by overriding `detail` key within our `OrderEventModel`
237237
4. Parser parsed the original event against `OrderEventModel`
238238

239+
???+ tip
240+
When extending a `string` field containing JSON, you need to wrap the field
241+
with [Pydantic's Json Type](https://pydantic-docs.helpmanual.io/usage/types/#json-type):
242+
243+
```python hl_lines="14 18-19"
244+
--8<-- "examples/parser/src/extending_built_in_models_with_json_mypy.py"
245+
```
246+
247+
Alternatively, you could use a [Pydantic validator](https://pydantic-docs.helpmanual.io/usage/validators/) to transform the JSON string into a dict before the mapping:
248+
249+
```python hl_lines="18-20 24-25"
250+
--8<-- "examples/parser/src/extending_built_in_models_with_json_validator.py"
251+
```
252+
239253
## Envelopes
240254

241255
When trying to parse your payloads wrapped in a known structure, you might encounter the following situations:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pydantic import BaseModel, Json
2+
3+
from aws_lambda_powertools.utilities.parser import event_parser
4+
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventV2Model
5+
from aws_lambda_powertools.utilities.typing import LambdaContext
6+
7+
8+
class CancelOrder(BaseModel):
9+
order_id: int
10+
reason: str
11+
12+
13+
class CancelOrderModel(APIGatewayProxyEventV2Model):
14+
body: Json[CancelOrder] # type: ignore[assignment]
15+
16+
17+
@event_parser(model=CancelOrderModel)
18+
def handler(event: CancelOrderModel, context: LambdaContext):
19+
cancel_order: CancelOrder = event.body # type: ignore[assignment]
20+
21+
assert cancel_order.order_id is not None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import json
2+
3+
from pydantic import BaseModel, validator
4+
5+
from aws_lambda_powertools.utilities.parser import event_parser
6+
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventV2Model
7+
from aws_lambda_powertools.utilities.typing import LambdaContext
8+
9+
10+
class CancelOrder(BaseModel):
11+
order_id: int
12+
reason: str
13+
14+
15+
class CancelOrderModel(APIGatewayProxyEventV2Model):
16+
body: CancelOrder # type: ignore[assignment]
17+
18+
@validator("body", pre=True)
19+
def transform_body_to_dict(cls, value: str):
20+
return json.loads(value)
21+
22+
23+
@event_parser(model=CancelOrderModel)
24+
def handler(event: CancelOrderModel, context: LambdaContext):
25+
cancel_order: CancelOrder = event.body
26+
27+
assert cancel_order.order_id is not None

0 commit comments

Comments
 (0)