|
2 | 2 | from datetime import datetime
|
3 | 3 | from typing import List
|
4 | 4 |
|
5 |
| -from pydantic import BaseModel |
| 5 | +from pydantic import BaseModel, Field |
6 | 6 | from typing_extensions import Annotated
|
7 | 7 |
|
8 | 8 | from aws_lambda_powertools.event_handler.api_gateway import APIGatewayRestResolver, Response, Router
|
@@ -495,3 +495,38 @@ def handler(
|
495 | 495 | assert parameter.schema_.exclusiveMaximum == 100
|
496 | 496 | assert len(parameter.schema_.examples) == 1
|
497 | 497 | assert parameter.schema_.examples[0] == "Example 1"
|
| 498 | + |
| 499 | + |
| 500 | +def test_openapi_with_examples_of_base_model_field(): |
| 501 | + app = APIGatewayRestResolver() |
| 502 | + |
| 503 | + class Todo(BaseModel): |
| 504 | + id: int = Field(examples=[1]) |
| 505 | + title: str = Field(examples=["Example 1"]) |
| 506 | + priority: float = Field(examples=[0.5]) |
| 507 | + completed: bool = Field(examples=[True]) |
| 508 | + |
| 509 | + @app.get("/") |
| 510 | + def handler() -> Todo: |
| 511 | + return Todo(id=0, title="", priority=0.0, completed=False) |
| 512 | + |
| 513 | + schema = app.get_openapi_schema() |
| 514 | + assert "Todo" in schema.components.schemas |
| 515 | + todo_schema = schema.components.schemas["Todo"] |
| 516 | + assert isinstance(todo_schema, Schema) |
| 517 | + |
| 518 | + assert "id" in todo_schema.properties |
| 519 | + id_property = todo_schema.properties["id"] |
| 520 | + assert id_property.examples == [1] |
| 521 | + |
| 522 | + assert "title" in todo_schema.properties |
| 523 | + title_property = todo_schema.properties["title"] |
| 524 | + assert title_property.examples == ["Example 1"] |
| 525 | + |
| 526 | + assert "priority" in todo_schema.properties |
| 527 | + priority_property = todo_schema.properties["priority"] |
| 528 | + assert priority_property.examples == [0.5] |
| 529 | + |
| 530 | + assert "completed" in todo_schema.properties |
| 531 | + completed_property = todo_schema.properties["completed"] |
| 532 | + assert completed_property.examples == [True] |
0 commit comments