Skip to content

Commit 55c22dc

Browse files
committed
Merge branch 'develop' into ryandeivert-firehose-event
2 parents e556593 + d9b80e4 commit 55c22dc

6 files changed

+87
-19
lines changed

Diff for: CHANGELOG.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88

99
* **typing:** level arg in copy_config_to_registered_loggers ([#1534](https://github.com/awslabs/aws-lambda-powertools-python/issues/1534))
1010

11+
## Documentation
12+
13+
* **parser:** add JSON string field extension example ([#1526](https://github.com/awslabs/aws-lambda-powertools-python/issues/1526))
14+
1115
## Maintenance
1216

13-
* **deps:** bump email-validator from 1.2.1 to 1.3.0 ([#1533](https://github.com/awslabs/aws-lambda-powertools-python/issues/1533))
14-
* **deps:** bump fastjsonschema from 2.16.1 to 2.16.2 ([#1530](https://github.com/awslabs/aws-lambda-powertools-python/issues/1530))
1517
* **deps:** bump codecov/codecov-action from 3.1.0 to 3.1.1 ([#1529](https://github.com/awslabs/aws-lambda-powertools-python/issues/1529))
1618
* **deps:** bump actions/setup-python from 3 to 4 ([#1528](https://github.com/awslabs/aws-lambda-powertools-python/issues/1528))
19+
* **deps:** bump email-validator from 1.2.1 to 1.3.0 ([#1533](https://github.com/awslabs/aws-lambda-powertools-python/issues/1533))
20+
* **deps:** bump fastjsonschema from 2.16.1 to 2.16.2 ([#1530](https://github.com/awslabs/aws-lambda-powertools-python/issues/1530))
1721
* **deps-dev:** bump mypy-boto3-s3 from 1.24.36.post1 to 1.24.76 ([#1531](https://github.com/awslabs/aws-lambda-powertools-python/issues/1531))
1822
* **deps-dev:** bump mkdocs-material from 8.5.1 to 8.5.3 ([#1532](https://github.com/awslabs/aws-lambda-powertools-python/issues/1532))
23+
* **deps-dev:** bump mako from 1.2.2 to 1.2.3 ([#1537](https://github.com/awslabs/aws-lambda-powertools-python/issues/1537))
24+
* **deps-dev:** bump types-requests from 2.28.10 to 2.28.11 ([#1538](https://github.com/awslabs/aws-lambda-powertools-python/issues/1538))
1925
* **docs:** bump layer version to 36 (1.29.2)
2026

2127

Diff for: 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

Diff for: poetry.lock

+13-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pyproject.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ mypy-boto3-dynamodb = { version = "^1.24.74", python = ">=3.7" }
6363
mypy-boto3-lambda = { version = "^1.24.0", python = ">=3.7" }
6464
mypy-boto3-logs = { version = "^1.24.0", python = ">=3.7" }
6565
mypy-boto3-secretsmanager = { version = "^1.24.11", python = ">=3.7" }
66-
mypy-boto3-ssm = { version = "^1.24.0", python = ">=3.7" }
66+
mypy-boto3-ssm = { version = "^1.24.80", python = ">=3.7" }
6767
mypy-boto3-s3 = { version = "^1.24.76", python = ">=3.7" }
6868
mypy-boto3-xray = { version = "^1.24.0", python = ">=3.7" }
69-
types-requests = "^2.28.8"
69+
types-requests = "^2.28.11"
7070
typing-extensions = { version = "^4.3.0", python = ">=3.7" }
7171
python-snappy = "^0.6.1"
7272
mkdocs-material = { version = "^8.5.3", python = ">=3.7" }
7373
filelock = { version = "^3.8.0", python = ">=3.7" }
7474
# Maintenance: 2022-09-19 pinned mako to fix vulnerability as a pdoc3 dependency. Remove once we drop python 3.6.
75-
Mako = {version = "1.2.2", python = ">=3.7"}
75+
Mako = {version = "1.2.3", python = ">=3.7"}
7676

7777
[tool.poetry.extras]
7878
pydantic = ["pydantic", "email-validator"]

0 commit comments

Comments
 (0)