From 372b7d22129c62e38604eeb1f2b9ca06cf857d37 Mon Sep 17 00:00:00 2001 From: kazutaka tonsho Date: Mon, 18 Nov 2024 14:03:44 +0000 Subject: [PATCH 1/3] Allow any values in the examples of the Schema Object --- .../event_handler/openapi/models.py | 2 +- .../_pydantic/test_openapi_params.py | 37 ++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/aws_lambda_powertools/event_handler/openapi/models.py b/aws_lambda_powertools/event_handler/openapi/models.py index 580e86f8112..f967e4c6bc0 100644 --- a/aws_lambda_powertools/event_handler/openapi/models.py +++ b/aws_lambda_powertools/event_handler/openapi/models.py @@ -201,7 +201,7 @@ class Schema(BaseModel): deprecated: Optional[bool] = None readOnly: Optional[bool] = None writeOnly: Optional[bool] = None - examples: Optional[Union[List["Example"], List[str]]] = None + examples: Optional[Union[List["Example"], List[Any]]] = None # Ref: OpenAPI 3.0.0: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#schema-object # Schema Object discriminator: Optional[Discriminator] = None diff --git a/tests/functional/event_handler/_pydantic/test_openapi_params.py b/tests/functional/event_handler/_pydantic/test_openapi_params.py index d838a0843e4..e3fafa96448 100644 --- a/tests/functional/event_handler/_pydantic/test_openapi_params.py +++ b/tests/functional/event_handler/_pydantic/test_openapi_params.py @@ -2,7 +2,7 @@ from datetime import datetime from typing import List -from pydantic import BaseModel +from pydantic import BaseModel, Field from typing_extensions import Annotated from aws_lambda_powertools.event_handler.api_gateway import APIGatewayRestResolver, Response, Router @@ -495,3 +495,38 @@ def handler( assert parameter.schema_.exclusiveMaximum == 100 assert len(parameter.schema_.examples) == 1 assert parameter.schema_.examples[0] == "Example 1" + + +def test_openapi_with_examples_of_base_model_field(): + app = 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.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] From ad218d7ee384f93b0e482bf3b4b59b9e85063560 Mon Sep 17 00:00:00 2001 From: kazutaka tonsho Date: Mon, 18 Nov 2024 14:10:20 +0000 Subject: [PATCH 2/3] temporarily avoid test failures --- .../event_handler/_pydantic/test_openapi_params.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/functional/event_handler/_pydantic/test_openapi_params.py b/tests/functional/event_handler/_pydantic/test_openapi_params.py index e3fafa96448..710627922f6 100644 --- a/tests/functional/event_handler/_pydantic/test_openapi_params.py +++ b/tests/functional/event_handler/_pydantic/test_openapi_params.py @@ -130,8 +130,9 @@ def handler( assert parameter.schema_.exclusiveMinimum == 0 assert parameter.schema_.exclusiveMaximum == 100 assert len(parameter.schema_.examples) == 1 - assert parameter.schema_.examples[0].summary == "Example 1" - assert parameter.schema_.examples[0].value == 10 + example = Example(**parameter.schema_.examples[0]) + assert example.summary == "Example 1" + assert example.value == 10 def test_openapi_with_scalar_returns(): From 7a96f4683bfdd42743517c0d0900e733c61f3665 Mon Sep 17 00:00:00 2001 From: Leandro Damascena Date: Mon, 18 Nov 2024 14:23:01 +0000 Subject: [PATCH 3/3] Simplify type annotation --- aws_lambda_powertools/event_handler/openapi/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws_lambda_powertools/event_handler/openapi/models.py b/aws_lambda_powertools/event_handler/openapi/models.py index f967e4c6bc0..afeb0a77750 100644 --- a/aws_lambda_powertools/event_handler/openapi/models.py +++ b/aws_lambda_powertools/event_handler/openapi/models.py @@ -201,7 +201,7 @@ class Schema(BaseModel): deprecated: Optional[bool] = None readOnly: Optional[bool] = None writeOnly: Optional[bool] = None - examples: Optional[Union[List["Example"], List[Any]]] = None + examples: Optional[List[Any]] = None # Ref: OpenAPI 3.0.0: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#schema-object # Schema Object discriminator: Optional[Discriminator] = None