Skip to content

Commit 41ca97b

Browse files
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 <[email protected]>
1 parent 5426a7a commit 41ca97b

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

aws_lambda_powertools/event_handler/openapi/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class Schema(BaseModel):
201201
deprecated: Optional[bool] = None
202202
readOnly: Optional[bool] = None
203203
writeOnly: Optional[bool] = None
204-
examples: Optional[Union[List["Example"], List[str]]] = None
204+
examples: Optional[List[Any]] = None
205205
# Ref: OpenAPI 3.0.0: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#schema-object
206206
# Schema Object
207207
discriminator: Optional[Discriminator] = None

tests/functional/event_handler/_pydantic/test_openapi_params.py

+39-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import datetime
33
from typing import List
44

5-
from pydantic import BaseModel
5+
from pydantic import BaseModel, Field
66
from typing_extensions import Annotated
77

88
from aws_lambda_powertools.event_handler.api_gateway import APIGatewayRestResolver, Response, Router
@@ -130,8 +130,9 @@ def handler(
130130
assert parameter.schema_.exclusiveMinimum == 0
131131
assert parameter.schema_.exclusiveMaximum == 100
132132
assert len(parameter.schema_.examples) == 1
133-
assert parameter.schema_.examples[0].summary == "Example 1"
134-
assert parameter.schema_.examples[0].value == 10
133+
example = Example(**parameter.schema_.examples[0])
134+
assert example.summary == "Example 1"
135+
assert example.value == 10
135136

136137

137138
def test_openapi_with_scalar_returns():
@@ -495,3 +496,38 @@ def handler(
495496
assert parameter.schema_.exclusiveMaximum == 100
496497
assert len(parameter.schema_.examples) == 1
497498
assert parameter.schema_.examples[0] == "Example 1"
499+
500+
501+
def test_openapi_with_examples_of_base_model_field():
502+
app = APIGatewayRestResolver()
503+
504+
class Todo(BaseModel):
505+
id: int = Field(examples=[1])
506+
title: str = Field(examples=["Example 1"])
507+
priority: float = Field(examples=[0.5])
508+
completed: bool = Field(examples=[True])
509+
510+
@app.get("/")
511+
def handler() -> Todo:
512+
return Todo(id=0, title="", priority=0.0, completed=False)
513+
514+
schema = app.get_openapi_schema()
515+
assert "Todo" in schema.components.schemas
516+
todo_schema = schema.components.schemas["Todo"]
517+
assert isinstance(todo_schema, Schema)
518+
519+
assert "id" in todo_schema.properties
520+
id_property = todo_schema.properties["id"]
521+
assert id_property.examples == [1]
522+
523+
assert "title" in todo_schema.properties
524+
title_property = todo_schema.properties["title"]
525+
assert title_property.examples == ["Example 1"]
526+
527+
assert "priority" in todo_schema.properties
528+
priority_property = todo_schema.properties["priority"]
529+
assert priority_property.examples == [0.5]
530+
531+
assert "completed" in todo_schema.properties
532+
completed_property = todo_schema.properties["completed"]
533+
assert completed_property.examples == [True]

0 commit comments

Comments
 (0)