|
1 | 1 | from secrets import randbelow
|
2 |
| -from typing import Union |
| 2 | +from typing import Optional, Union |
3 | 3 |
|
4 | 4 | from pydantic import BaseModel
|
5 | 5 |
|
@@ -237,3 +237,136 @@ def handler():
|
237 | 237 | assert 200 in responses.keys()
|
238 | 238 | assert responses[200].description == "Successful Response"
|
239 | 239 | assert 422 not in responses.keys()
|
| 240 | + |
| 241 | + |
| 242 | +def test_openapi_response_with_headers(): |
| 243 | + """Test that response headers are properly included in OpenAPI schema""" |
| 244 | + app = APIGatewayRestResolver(enable_validation=True) |
| 245 | + |
| 246 | + @app.get( |
| 247 | + "/", |
| 248 | + responses={ |
| 249 | + 200: { |
| 250 | + "description": "Successful Response", |
| 251 | + "headers": { |
| 252 | + "X-Rate-Limit": { |
| 253 | + "description": "Rate limit header", |
| 254 | + "schema": {"type": "integer"}, |
| 255 | + }, |
| 256 | + "X-Custom-Header": { |
| 257 | + "description": "Custom header", |
| 258 | + "schema": {"type": "string"}, |
| 259 | + "examples": {"example1": "value1"}, |
| 260 | + }, |
| 261 | + }, |
| 262 | + }, |
| 263 | + }, |
| 264 | + ) |
| 265 | + def handler(): |
| 266 | + return {"message": "hello"} |
| 267 | + |
| 268 | + schema = app.get_openapi_schema() |
| 269 | + response_dict = schema.paths["/"].get.responses[200] |
| 270 | + |
| 271 | + # Verify headers are present |
| 272 | + assert "headers" in response_dict |
| 273 | + headers = response_dict["headers"] |
| 274 | + |
| 275 | + # Check X-Rate-Limit header |
| 276 | + assert "X-Rate-Limit" in headers |
| 277 | + assert headers["X-Rate-Limit"]["description"] == "Rate limit header" |
| 278 | + assert headers["X-Rate-Limit"]["schema"]["type"] == "integer" |
| 279 | + |
| 280 | + # Check X-Custom-Header with examples |
| 281 | + assert "X-Custom-Header" in headers |
| 282 | + assert headers["X-Custom-Header"]["description"] == "Custom header" |
| 283 | + assert headers["X-Custom-Header"]["schema"]["type"] == "string" |
| 284 | + assert headers["X-Custom-Header"]["examples"]["example1"] == "value1" |
| 285 | + |
| 286 | + |
| 287 | +def test_openapi_response_with_links(): |
| 288 | + """Test that response links are properly included in OpenAPI schema""" |
| 289 | + app = APIGatewayRestResolver(enable_validation=True) |
| 290 | + |
| 291 | + @app.get( |
| 292 | + "/users/{user_id}", |
| 293 | + responses={ |
| 294 | + 200: { |
| 295 | + "description": "User details", |
| 296 | + "links": { |
| 297 | + "GetUserOrders": { |
| 298 | + "operationId": "getUserOrders", |
| 299 | + "parameters": {"userId": "$response.body#/id"}, |
| 300 | + "description": "Get orders for this user", |
| 301 | + }, |
| 302 | + }, |
| 303 | + }, |
| 304 | + }, |
| 305 | + ) |
| 306 | + def get_user(user_id: str): |
| 307 | + return {"id": user_id, "name": "John Doe"} |
| 308 | + |
| 309 | + schema = app.get_openapi_schema() |
| 310 | + response = schema.paths["/users/{user_id}"].get.responses[200] |
| 311 | + |
| 312 | + # Verify links are present |
| 313 | + links = response.links |
| 314 | + |
| 315 | + assert "GetUserOrders" in links |
| 316 | + assert links["GetUserOrders"].operationId == "getUserOrders" |
| 317 | + assert links["GetUserOrders"].parameters["userId"] == "$response.body#/id" |
| 318 | + assert links["GetUserOrders"].description == "Get orders for this user" |
| 319 | + |
| 320 | + |
| 321 | +def test_openapi_response_examples_preserved_with_model(): |
| 322 | + """Test that examples are preserved when using model in response content""" |
| 323 | + app = APIGatewayRestResolver(enable_validation=True) |
| 324 | + |
| 325 | + class UserResponse(BaseModel): |
| 326 | + id: int |
| 327 | + name: str |
| 328 | + email: Optional[str] = None |
| 329 | + |
| 330 | + @app.get( |
| 331 | + "/", |
| 332 | + responses={ |
| 333 | + 200: { |
| 334 | + "description": "User response", |
| 335 | + "content": { |
| 336 | + "application/json": { |
| 337 | + "model": UserResponse, |
| 338 | + "examples": { |
| 339 | + "example1": { |
| 340 | + "summary": "Example 1", |
| 341 | + "value": {"id": 1, "name": "John", "email": "[email protected]"}, |
| 342 | + }, |
| 343 | + "example2": { |
| 344 | + "summary": "Example 2", |
| 345 | + "value": {"id": 2, "name": "Jane"}, |
| 346 | + }, |
| 347 | + }, |
| 348 | + }, |
| 349 | + }, |
| 350 | + }, |
| 351 | + }, |
| 352 | + ) |
| 353 | + def handler() -> UserResponse: |
| 354 | + return UserResponse(id=1, name="Test") |
| 355 | + |
| 356 | + schema = app.get_openapi_schema() |
| 357 | + content = schema.paths["/"].get.responses[200].content["application/json"] |
| 358 | + |
| 359 | + # Verify model schema is present |
| 360 | + assert content.schema_.ref == "#/components/schemas/UserResponse" |
| 361 | + |
| 362 | + # Verify examples are preserved |
| 363 | + examples = content.examples |
| 364 | + |
| 365 | + assert "example1" in examples |
| 366 | + assert examples["example1"].summary == "Example 1" |
| 367 | + assert examples["example1"].value["id"] == 1 |
| 368 | + assert examples["example1"].value["name"] == "John" |
| 369 | + |
| 370 | + assert "example2" in examples |
| 371 | + assert examples["example2"].summary == "Example 2" |
| 372 | + assert examples["example2"].value["id"] == 2 |
0 commit comments