From 41ca97b3b6fb40adccd34eb62ef0bc1b7fb4576b Mon Sep 17 00:00:00 2001 From: tonsho Date: Tue, 19 Nov 2024 19:37:00 +0900 Subject: [PATCH] fix(openapi): Allow values of any type in the examples of the Schema Object. (#5575) * Allow any values in the examples of the Schema Object * temporarily avoid test failures * Simplify type annotation --------- Co-authored-by: Leandro Damascena --- .../event_handler/openapi/models.py | 2 +- .../_pydantic/test_openapi_params.py | 42 +++++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/aws_lambda_powertools/event_handler/openapi/models.py b/aws_lambda_powertools/event_handler/openapi/models.py index 580e86f8112..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[str]]] = 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 diff --git a/tests/functional/event_handler/_pydantic/test_openapi_params.py b/tests/functional/event_handler/_pydantic/test_openapi_params.py index d838a0843e4..710627922f6 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 @@ -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(): @@ -495,3 +496,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]