|
4 | 4 | import warnings |
5 | 5 |
|
6 | 6 | import pytest |
| 7 | +from pydantic import BaseModel, ConfigDict |
7 | 8 |
|
8 | 9 | from aws_lambda_powertools.event_handler import APIGatewayRestResolver |
9 | 10 | from aws_lambda_powertools.event_handler.openapi.swagger_ui import OAuth2Config |
@@ -125,3 +126,40 @@ def test_openapi_swagger_oauth2_with_powertools_dev(monkeypatch): |
125 | 126 | ) |
126 | 127 |
|
127 | 128 | monkeypatch.delenv("POWERTOOLS_DEV") |
| 129 | + |
| 130 | + |
| 131 | +def test_openapi_swagger_schema_title_property(): |
| 132 | + """Test that schema title property is correctly included in OpenAPI spec. |
| 133 | +
|
| 134 | + This test ensures that when a Pydantic model has a custom title defined |
| 135 | + via ConfigDict, it is properly reflected in the OpenAPI schema. |
| 136 | + See: https://github.com/aws-powertools/powertools-lambda-python/issues/7670 |
| 137 | + """ |
| 138 | + app = APIGatewayRestResolver(enable_validation=True) |
| 139 | + app.enable_swagger() |
| 140 | + |
| 141 | + @app.get("/todos") |
| 142 | + def get_todos() -> ToDoWithTitle: |
| 143 | + return ToDoWithTitle(task="test") |
| 144 | + |
| 145 | + event = load_event("apiGatewayProxyEvent.json") |
| 146 | + event["path"] = "/swagger" |
| 147 | + event["queryStringParameters"] = {"format": "json"} |
| 148 | + |
| 149 | + result = app(event, {}) |
| 150 | + |
| 151 | + assert result["statusCode"] == 200 |
| 152 | + body = json.loads(result["body"]) |
| 153 | + |
| 154 | + # Verify the schema has the title property set |
| 155 | + assert "components" in body |
| 156 | + assert "schemas" in body["components"] |
| 157 | + assert "ToDoWithTitle" in body["components"]["schemas"] |
| 158 | + assert body["components"]["schemas"]["ToDoWithTitle"]["title"] == "todoTitle" |
| 159 | + |
| 160 | + |
| 161 | +class ToDoWithTitle(BaseModel): |
| 162 | + """Model with custom title for testing schema title property.""" |
| 163 | + |
| 164 | + model_config = ConfigDict(title="todoTitle") |
| 165 | + task: str |
0 commit comments