generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 455
feat(event-handler): add support for Pydantic Field discriminator in validation #7227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f6ca987
feat(event-handler): add support for Pydantic Field discriminator in …
dap0am 63a0225
style(tests): remove inline comments to match project test style
dap0am d35753c
Merge branch 'develop' into develop
leandrodamascena 0023b3a
style: run make format to fix CI formatting issues
dap0am 62bb9c9
Merge branch 'develop' into develop
leandrodamascena ead3ee8
fix(event-handler): preserve FieldInfo subclass types in copy_field_info
dap0am 0c3aad6
Merge branch 'develop' into develop
dreamorosi e2c8b49
refactor(event-handler): reduce cognitive complexity and address Sona…
dap0am d61e531
Merge branch 'develop' into develop
leandrodamascena 95a5eba
style: fix formatting to pass CI format check
dap0am 913c580
Merge branch 'develop' into develop
leandrodamascena d839919
fix: resolve mypy type error in _create_field_info function
dap0am 3a53a67
Merge branch 'develop' into develop
dreamorosi 5762c94
fix: use Union syntax for Python 3.9 compatibility
dap0am 07ade23
Merge branch 'develop' into develop
leandrodamascena 89fb5f8
Merge branch 'develop' into develop
leandrodamascena 793a097
feat(event-handler): add documentation and example for Field discrimi…
dap0am 9eac24a
Merge branch 'develop' into develop
leandrodamascena 4400181
style: run make format to fix CI formatting issues
dap0am 5cc265e
Merge branch 'develop' into develop
leandrodamascena 587d2fa
small changes
leandrodamascena 96ab0a6
Merge branch 'develop' into develop
leandrodamascena 851992f
small changes
leandrodamascena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,10 @@ | |
from dataclasses import dataclass | ||
from enum import Enum | ||
from pathlib import PurePath | ||
from typing import List, Optional, Tuple | ||
from typing import List, Literal, Optional, Tuple, Union | ||
|
||
import pytest | ||
from pydantic import BaseModel | ||
from pydantic import BaseModel, Field | ||
from typing_extensions import Annotated | ||
|
||
from aws_lambda_powertools.event_handler import ( | ||
|
@@ -1983,3 +1983,82 @@ def get_user(user_id: int) -> UserModel: | |
assert response_body["name"] == "User123" | ||
assert response_body["age"] == 143 | ||
assert response_body["email"] == "[email protected]" | ||
|
||
|
||
def test_field_discriminator_validation(gw_event): | ||
"""Test that Pydantic Field discriminator works with event_handler validation""" | ||
app = APIGatewayRestResolver(enable_validation=True) | ||
|
||
class FooAction(BaseModel): | ||
action: Literal["foo"] | ||
foo_data: str | ||
|
||
class BarAction(BaseModel): | ||
action: Literal["bar"] | ||
bar_data: int | ||
|
||
action_type = Annotated[Union[FooAction, BarAction], Field(discriminator="action")] | ||
|
||
@app.post("/actions") | ||
def create_action(action: Annotated[action_type, Body()]): | ||
return {"received_action": action.action, "data": action.model_dump()} | ||
|
||
gw_event["path"] = "/actions" | ||
gw_event["httpMethod"] = "POST" | ||
gw_event["headers"]["content-type"] = "application/json" | ||
gw_event["body"] = '{"action": "foo", "foo_data": "test"}' | ||
|
||
result = app(gw_event, {}) | ||
assert result["statusCode"] == 200 | ||
|
||
response_body = json.loads(result["body"]) | ||
assert response_body["received_action"] == "foo" | ||
assert response_body["data"]["action"] == "foo" | ||
assert response_body["data"]["foo_data"] == "test" | ||
|
||
gw_event["body"] = '{"action": "bar", "bar_data": 123}' | ||
|
||
result = app(gw_event, {}) | ||
assert result["statusCode"] == 200 | ||
|
||
response_body = json.loads(result["body"]) | ||
assert response_body["received_action"] == "bar" | ||
assert response_body["data"]["action"] == "bar" | ||
assert response_body["data"]["bar_data"] == 123 | ||
|
||
gw_event["body"] = '{"action": "invalid", "some_data": "test"}' | ||
|
||
result = app(gw_event, {}) | ||
assert result["statusCode"] == 422 | ||
|
||
|
||
def test_field_other_features_still_work(gw_event): | ||
"""Test that other Field features still work after discriminator fix""" | ||
app = APIGatewayRestResolver(enable_validation=True) | ||
|
||
class UserInput(BaseModel): | ||
name: Annotated[str, Field(min_length=2, max_length=50, description="User name")] | ||
age: Annotated[int, Field(ge=18, le=120, description="User age")] | ||
email: Annotated[str, Field(pattern=r".+@.+\..+", description="User email")] | ||
|
||
@app.post("/users") | ||
def create_user(user: UserInput): | ||
return {"created": user.model_dump()} | ||
|
||
gw_event["path"] = "/users" | ||
gw_event["httpMethod"] = "POST" | ||
gw_event["headers"]["content-type"] = "application/json" | ||
gw_event["body"] = '{"name": "John", "age": 25, "email": "[email protected]"}' | ||
|
||
result = app(gw_event, {}) | ||
assert result["statusCode"] == 200 | ||
|
||
response_body = json.loads(result["body"]) | ||
assert response_body["created"]["name"] == "John" | ||
assert response_body["created"]["age"] == 25 | ||
assert response_body["created"]["email"] == "[email protected]" | ||
|
||
gw_event["body"] = '{"name": "John", "age": 16, "email": "[email protected]"}' | ||
|
||
result = app(gw_event, {}) | ||
assert result["statusCode"] == 422 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.