-
Notifications
You must be signed in to change notification settings - Fork 433
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
tests/e2e/event_handler/handlers/openapi_handler_with_pep563.py
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from __future__ import annotations | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from aws_lambda_powertools.event_handler import ( | ||
APIGatewayRestResolver, | ||
) | ||
|
||
|
||
class Todo(BaseModel): | ||
id: int = Field(examples=[1]) | ||
title: str = Field(examples=["Example 1"]) | ||
priority: float = Field(examples=[0.5]) | ||
completed: bool = Field(examples=[True]) | ||
|
||
|
||
app = APIGatewayRestResolver(enable_validation=True) | ||
|
||
|
||
@app.get("/openapi_schema_with_pep563") | ||
def openapi_schema(): | ||
return app.get_openapi_json_schema( | ||
title="Powertools e2e API", | ||
version="1.0.0", | ||
description="This is a sample Powertools e2e API", | ||
openapi_extensions={"x-amazon-apigateway-gateway-responses": {"DEFAULT_4XX"}}, | ||
) | ||
|
||
|
||
@app.get("/") | ||
def handler() -> Todo: | ||
return Todo(id=0, title="", priority=0.0, completed=False) | ||
|
||
|
||
def lambda_handler(event, context): | ||
return app.resolve(event, context) |
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
120 changes: 120 additions & 0 deletions
120
tests/functional/event_handler/_pydantic/test_openapi_with_pep563.py
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 |
---|---|---|
@@ -0,0 +1,120 @@ | ||
from __future__ import annotations | ||
|
||
from pydantic import BaseModel, Field | ||
from typing_extensions import Annotated | ||
|
||
from aws_lambda_powertools.event_handler.api_gateway import APIGatewayRestResolver | ||
from aws_lambda_powertools.event_handler.openapi.models import ( | ||
ParameterInType, | ||
Schema, | ||
) | ||
from aws_lambda_powertools.event_handler.openapi.params import ( | ||
Body, | ||
Query, | ||
) | ||
|
||
JSON_CONTENT_TYPE = "application/json" | ||
|
||
|
||
class Todo(BaseModel): | ||
id: int = Field(examples=[1]) | ||
title: str = Field(examples=["Example 1"]) | ||
priority: float = Field(examples=[0.5]) | ||
completed: bool = Field(examples=[True]) | ||
|
||
|
||
def test_openapi_with_pep563_and_input_model(): | ||
app = APIGatewayRestResolver() | ||
|
||
@app.get("/users", summary="Get Users", operation_id="GetUsers", description="Get paginated users", tags=["Users"]) | ||
def handler( | ||
count: Annotated[ | ||
int, | ||
Query(gt=0, lt=100, examples=["Example 1"]), | ||
] = 1, | ||
): | ||
print(count) | ||
raise NotImplementedError() | ||
|
||
schema = app.get_openapi_schema() | ||
|
||
get = schema.paths["/users"].get | ||
assert len(get.parameters) == 1 | ||
assert get.summary == "Get Users" | ||
assert get.operationId == "GetUsers" | ||
assert get.description == "Get paginated users" | ||
assert get.tags == ["Users"] | ||
|
||
parameter = get.parameters[0] | ||
assert parameter.required is False | ||
assert parameter.name == "count" | ||
assert parameter.in_ == ParameterInType.query | ||
assert parameter.schema_.type == "integer" | ||
assert parameter.schema_.default == 1 | ||
assert parameter.schema_.title == "Count" | ||
assert parameter.schema_.exclusiveMinimum == 0 | ||
assert parameter.schema_.exclusiveMaximum == 100 | ||
assert len(parameter.schema_.examples) == 1 | ||
assert parameter.schema_.examples[0] == "Example 1" | ||
|
||
|
||
def test_openapi_with_pep563_and_output_model(): | ||
|
||
app = APIGatewayRestResolver() | ||
|
||
@app.get("/") | ||
def handler() -> Todo: | ||
return Todo(id=0, title="", priority=0.0, completed=False) | ||
|
||
schema = app.get_openapi_schema() | ||
assert "Todo" in schema.components.schemas | ||
todo_schema = schema.components.schemas["Todo"] | ||
assert isinstance(todo_schema, Schema) | ||
|
||
assert "id" in todo_schema.properties | ||
id_property = todo_schema.properties["id"] | ||
assert id_property.examples == [1] | ||
|
||
assert "title" in todo_schema.properties | ||
title_property = todo_schema.properties["title"] | ||
assert title_property.examples == ["Example 1"] | ||
|
||
assert "priority" in todo_schema.properties | ||
priority_property = todo_schema.properties["priority"] | ||
assert priority_property.examples == [0.5] | ||
|
||
assert "completed" in todo_schema.properties | ||
completed_property = todo_schema.properties["completed"] | ||
assert completed_property.examples == [True] | ||
|
||
|
||
def test_openapi_with_pep563_and_annotated_body(): | ||
|
||
app = APIGatewayRestResolver() | ||
|
||
@app.post("/todo") | ||
def create_todo( | ||
todo_create_request: Annotated[Todo, Body(title="New Todo")], | ||
) -> dict: | ||
return {"message": f"Created todo {todo_create_request.title}"} | ||
|
||
schema = app.get_openapi_schema() | ||
assert "Todo" in schema.components.schemas | ||
todo_schema = schema.components.schemas["Todo"] | ||
assert isinstance(todo_schema, Schema) | ||
|
||
assert "id" in todo_schema.properties | ||
id_property = todo_schema.properties["id"] | ||
assert id_property.examples == [1] | ||
|
||
assert "title" in todo_schema.properties | ||
title_property = todo_schema.properties["title"] | ||
assert title_property.examples == ["Example 1"] | ||
|
||
assert "priority" in todo_schema.properties | ||
priority_property = todo_schema.properties["priority"] | ||
assert priority_property.examples == [0.5] | ||
|
||
assert "completed" in todo_schema.properties | ||
completed_property = todo_schema.properties["completed"] | ||
assert completed_property.examples == [True] |
Oops, something went wrong.
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.
fix(event_handler): add tests for PEP 563 compatibility with OpenAPI #5886
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
Uh oh!
There was an error while loading. Please reload this page.
fix(event_handler): add tests for PEP 563 compatibility with OpenAPI #5886
Changes from all commits
3b4b989
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.