Skip to content

Commit

Permalink
fix(openapi): Allow values of any type in the examples of the Schema …
Browse files Browse the repository at this point in the history
…Object. (#5575)

* Allow any values in the examples of the Schema Object

* temporarily avoid test failures

* Simplify type annotation

---------

Co-authored-by: Leandro Damascena <[email protected]>
  • Loading branch information
tonsho and leandrodamascena authored Nov 19, 2024
1 parent 5426a7a commit 41ca97b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion aws_lambda_powertools/event_handler/openapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 39 additions & 3 deletions tests/functional/event_handler/_pydantic/test_openapi_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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]

0 comments on commit 41ca97b

Please sign in to comment.