|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pydantic import BaseModel, Field |
| 4 | +from typing_extensions import Annotated |
| 5 | + |
| 6 | +from aws_lambda_powertools.event_handler.api_gateway import APIGatewayRestResolver |
| 7 | +from aws_lambda_powertools.event_handler.openapi.models import ( |
| 8 | + ParameterInType, |
| 9 | + Schema, |
| 10 | +) |
| 11 | +from aws_lambda_powertools.event_handler.openapi.params import ( |
| 12 | + Body, |
| 13 | + Query, |
| 14 | +) |
| 15 | + |
| 16 | +JSON_CONTENT_TYPE = "application/json" |
| 17 | + |
| 18 | + |
| 19 | +class Todo(BaseModel): |
| 20 | + id: int = Field(examples=[1]) |
| 21 | + title: str = Field(examples=["Example 1"]) |
| 22 | + priority: float = Field(examples=[0.5]) |
| 23 | + completed: bool = Field(examples=[True]) |
| 24 | + |
| 25 | + |
| 26 | +def test_openapi_with_pep563_and_input_model(): |
| 27 | + app = APIGatewayRestResolver() |
| 28 | + |
| 29 | + @app.get("/users", summary="Get Users", operation_id="GetUsers", description="Get paginated users", tags=["Users"]) |
| 30 | + def handler( |
| 31 | + count: Annotated[ |
| 32 | + int, |
| 33 | + Query(gt=0, lt=100, examples=["Example 1"]), |
| 34 | + ] = 1, |
| 35 | + ): |
| 36 | + print(count) |
| 37 | + raise NotImplementedError() |
| 38 | + |
| 39 | + schema = app.get_openapi_schema() |
| 40 | + |
| 41 | + get = schema.paths["/users"].get |
| 42 | + assert len(get.parameters) == 1 |
| 43 | + assert get.summary == "Get Users" |
| 44 | + assert get.operationId == "GetUsers" |
| 45 | + assert get.description == "Get paginated users" |
| 46 | + assert get.tags == ["Users"] |
| 47 | + |
| 48 | + parameter = get.parameters[0] |
| 49 | + assert parameter.required is False |
| 50 | + assert parameter.name == "count" |
| 51 | + assert parameter.in_ == ParameterInType.query |
| 52 | + assert parameter.schema_.type == "integer" |
| 53 | + assert parameter.schema_.default == 1 |
| 54 | + assert parameter.schema_.title == "Count" |
| 55 | + assert parameter.schema_.exclusiveMinimum == 0 |
| 56 | + assert parameter.schema_.exclusiveMaximum == 100 |
| 57 | + assert len(parameter.schema_.examples) == 1 |
| 58 | + assert parameter.schema_.examples[0] == "Example 1" |
| 59 | + |
| 60 | + |
| 61 | +def test_openapi_with_pep563_and_output_model(): |
| 62 | + |
| 63 | + app = APIGatewayRestResolver() |
| 64 | + |
| 65 | + @app.get("/") |
| 66 | + def handler() -> Todo: |
| 67 | + return Todo(id=0, title="", priority=0.0, completed=False) |
| 68 | + |
| 69 | + schema = app.get_openapi_schema() |
| 70 | + assert "Todo" in schema.components.schemas |
| 71 | + todo_schema = schema.components.schemas["Todo"] |
| 72 | + assert isinstance(todo_schema, Schema) |
| 73 | + |
| 74 | + assert "id" in todo_schema.properties |
| 75 | + id_property = todo_schema.properties["id"] |
| 76 | + assert id_property.examples == [1] |
| 77 | + |
| 78 | + assert "title" in todo_schema.properties |
| 79 | + title_property = todo_schema.properties["title"] |
| 80 | + assert title_property.examples == ["Example 1"] |
| 81 | + |
| 82 | + assert "priority" in todo_schema.properties |
| 83 | + priority_property = todo_schema.properties["priority"] |
| 84 | + assert priority_property.examples == [0.5] |
| 85 | + |
| 86 | + assert "completed" in todo_schema.properties |
| 87 | + completed_property = todo_schema.properties["completed"] |
| 88 | + assert completed_property.examples == [True] |
| 89 | + |
| 90 | + |
| 91 | +def test_openapi_with_pep563_and_annotated_body(): |
| 92 | + |
| 93 | + app = APIGatewayRestResolver() |
| 94 | + |
| 95 | + @app.post("/todo") |
| 96 | + def create_todo( |
| 97 | + todo_create_request: Annotated[Todo, Body(title="New Todo")], |
| 98 | + ) -> dict: |
| 99 | + return {"message": f"Created todo {todo_create_request.title}"} |
| 100 | + |
| 101 | + schema = app.get_openapi_schema() |
| 102 | + assert "Todo" in schema.components.schemas |
| 103 | + todo_schema = schema.components.schemas["Todo"] |
| 104 | + assert isinstance(todo_schema, Schema) |
| 105 | + |
| 106 | + assert "id" in todo_schema.properties |
| 107 | + id_property = todo_schema.properties["id"] |
| 108 | + assert id_property.examples == [1] |
| 109 | + |
| 110 | + assert "title" in todo_schema.properties |
| 111 | + title_property = todo_schema.properties["title"] |
| 112 | + assert title_property.examples == ["Example 1"] |
| 113 | + |
| 114 | + assert "priority" in todo_schema.properties |
| 115 | + priority_property = todo_schema.properties["priority"] |
| 116 | + assert priority_property.examples == [0.5] |
| 117 | + |
| 118 | + assert "completed" in todo_schema.properties |
| 119 | + completed_property = todo_schema.properties["completed"] |
| 120 | + assert completed_property.examples == [True] |
0 commit comments