22
33from typing import Optional
44
5- import pytest
65from pydantic import BaseModel
76
8- from aws_lambda_powertools .event_handler import APIGatewayRestResolver , Response
7+ from aws_lambda_powertools .event_handler import APIGatewayRestResolver
98from aws_lambda_powertools .event_handler .router import Router
109
1110
@@ -29,24 +28,24 @@ def test_openapi_response_with_headers():
2928 "examples" : {"example1" : "value1" },
3029 },
3130 },
32- }
31+ },
3332 },
3433 )
3534 def handler ():
3635 return {"message" : "hello" }
3736
3837 schema = app .get_openapi_schema ()
3938 response_dict = schema .paths ["/" ].get .responses [200 ]
40-
39+
4140 # Verify headers are present
4241 assert "headers" in response_dict
4342 headers = response_dict ["headers" ]
44-
43+
4544 # Check X-Rate-Limit header
4645 assert "X-Rate-Limit" in headers
4746 assert headers ["X-Rate-Limit" ]["description" ] == "Rate limit header"
4847 assert headers ["X-Rate-Limit" ]["schema" ]["type" ] == "integer"
49-
48+
5049 # Check X-Custom-Header with examples
5150 assert "X-Custom-Header" in headers
5251 assert headers ["X-Custom-Header" ]["description" ] == "Custom header"
@@ -68,20 +67,20 @@ def test_openapi_response_with_links():
6867 "operationId" : "getUserOrders" ,
6968 "parameters" : {"userId" : "$response.body#/id" },
7069 "description" : "Get orders for this user" ,
71- }
70+ },
7271 },
73- }
72+ },
7473 },
7574 )
7675 def get_user (user_id : str ):
7776 return {"id" : user_id , "name" : "John Doe" }
7877
7978 schema = app .get_openapi_schema ()
8079 response = schema .paths ["/users/{user_id}" ].get .responses [200 ]
81-
80+
8281 # Verify links are present
8382 links = response .links
84-
83+
8584 assert "GetUserOrders" in links
8685 assert links ["GetUserOrders" ].operationId == "getUserOrders"
8786 assert links ["GetUserOrders" ].parameters ["userId" ] == "$response.body#/id"
@@ -115,28 +114,28 @@ class UserResponse(BaseModel):
115114 "value" : {"id" : 2 , "name" : "Jane" },
116115 },
117116 },
118- }
117+ },
119118 },
120- }
119+ },
121120 },
122121 )
123122 def handler () -> UserResponse :
124123 return UserResponse (id = 1 , name = "Test" )
125124
126125 schema = app .get_openapi_schema ()
127126 content = schema .paths ["/" ].get .responses [200 ].content ["application/json" ]
128-
127+
129128 # Verify model schema is present
130129 assert content .schema_ .ref == "#/components/schemas/UserResponse"
131-
130+
132131 # Verify examples are preserved
133132 examples = content .examples
134-
133+
135134 assert "example1" in examples
136135 assert examples ["example1" ].summary == "Example 1"
137136 assert examples ["example1" ].value ["id" ] == 1
138137 assert examples ["example1" ].value ["name" ] == "John"
139-
138+
140139 assert "example2" in examples
141140 assert examples ["example2" ].summary == "Example 2"
142141 assert examples ["example2" ].value ["id" ] == 2
@@ -166,27 +165,27 @@ class FileUploadResponse(BaseModel):
166165 "X-Custom-Header" : {
167166 "description" : "Custom encoding header" ,
168167 "schema" : {"type" : "string" },
169- }
168+ },
170169 },
171- }
170+ },
172171 },
173- }
172+ },
174173 },
175- }
174+ },
176175 },
177176 )
178177 def upload_file () -> FileUploadResponse :
179178 return FileUploadResponse (file_id = "123" , filename = "test.pdf" , content = b"" )
180179
181180 schema = app .get_openapi_schema ()
182181 content = schema .paths ["/upload" ].post .responses [200 ].content ["multipart/form-data" ]
183-
182+
184183 # Verify model schema is present
185184 assert content .schema_ .ref == "#/components/schemas/FileUploadResponse"
186-
185+
187186 # Verify encoding is preserved
188187 encoding = content .encoding
189-
188+
190189 assert "content" in encoding
191190 assert encoding ["content" ].contentType == "application/octet-stream"
192191 assert encoding ["content" ].headers is not None
@@ -223,40 +222,40 @@ class DataResponse(BaseModel):
223222 "success" : {
224223 "summary" : "Successful response" ,
225224 "value" : {"data" : "test" , "timestamp" : 1234567890 },
226- }
225+ },
227226 },
228227 "encoding" : {
229228 "data" : {
230229 "contentType" : "text/plain" ,
231- }
230+ },
232231 },
233- }
232+ },
234233 },
235234 "links" : {
236235 "next" : {
237236 "operationId" : "getNextPage" ,
238237 "parameters" : {"page" : "$response.headers.X-Page + 1" },
239- }
238+ },
240239 },
241- }
240+ },
242241 },
243242 )
244243 def get_data () -> DataResponse :
245244 return DataResponse (data = "test" , timestamp = 1234567890 )
246245
247246 schema = app .get_openapi_schema ()
248247 response = schema .paths ["/data" ].get .responses [200 ]
249-
248+
250249 # Check headers
251250 assert "X-Total-Count" in response .headers
252251 assert "X-Page" in response .headers
253-
252+
254253 # Check content with model, examples, and encoding
255254 content = response .content ["application/json" ]
256255 assert content .schema_ .ref == "#/components/schemas/DataResponse"
257256 assert "success" in content .examples
258257 assert "data" in content .encoding
259-
258+
260259 # Check links
261260 assert "next" in response .links
262261 assert response .links ["next" ].operationId == "getNextPage"
@@ -281,7 +280,7 @@ def simple_handler():
281280 200 : {
282281 "description" : "With model" ,
283282 "content" : {"application/json" : {"model" : SimpleResponse }},
284- }
283+ },
285284 },
286285 )
287286 def model_handler () -> SimpleResponse :
@@ -294,31 +293,29 @@ def model_handler() -> SimpleResponse:
294293 200 : {
295294 "description" : "With schema" ,
296295 "content" : {
297- "application/json" : {
298- "schema" : {"type" : "object" , "properties" : {"msg" : {"type" : "string" }}}
299- }
296+ "application/json" : {"schema" : {"type" : "object" , "properties" : {"msg" : {"type" : "string" }}}},
300297 },
301- }
298+ },
302299 },
303300 )
304301 def schema_handler ():
305302 return {"msg" : "test" }
306303
307304 schema = app .get_openapi_schema ()
308-
305+
309306 # Verify all endpoints work
310307 assert "/simple" in schema .paths
311308 assert "/with-model" in schema .paths
312309 assert "/with-schema" in schema .paths
313-
310+
314311 # Check simple response
315312 simple_response = schema .paths ["/simple" ].get .responses [200 ]
316313 assert simple_response .description == "Simple response"
317-
314+
318315 # Check model response
319316 model_response = schema .paths ["/with-model" ].get .responses [200 ]
320317 assert model_response .content ["application/json" ].schema_ .ref == "#/components/schemas/SimpleResponse"
321-
318+
322319 # Check schema response
323320 schema_response = schema .paths ["/with-schema" ].get .responses [200 ]
324321 assert schema_response .content ["application/json" ].schema_ .type == "object"
@@ -340,23 +337,23 @@ def test_openapi_response_empty_optional_fields():
340337 "schema" : {"type" : "object" },
341338 "examples" : {}, # Empty examples
342339 "encoding" : {}, # Empty encoding
343- }
340+ },
344341 },
345- }
342+ },
346343 },
347344 )
348345 def empty_handler ():
349346 return {}
350347
351348 schema = app .get_openapi_schema ()
352349 response = schema .paths ["/empty" ].get .responses [200 ]
353-
350+
354351 # Empty dicts should still be present in the schema
355352 assert response .headers == {}
356353 assert response .links == {}
357-
354+
358355 content = response .content ["application/json" ]
359-
356+
360357 # Check if examples and encoding are empty or None (both are valid)
361358 assert content .examples == {} or content .examples is None
362359 assert content .encoding == {} or content .encoding is None
@@ -394,25 +391,25 @@ class JsonResponse(BaseModel):
394391 },
395392 },
396393 },
397- }
394+ },
398395 },
399396 )
400397 def multi_content_handler ():
401398 return {"data" : "test" }
402399
403400 schema = app .get_openapi_schema ()
404401 response = schema .paths ["/multi-content" ].get .responses [200 ]
405-
402+
406403 # Check JSON content
407404 json_content = response .content ["application/json" ]
408405 assert json_content .schema_ .ref == "#/components/schemas/JsonResponse"
409406 assert "json_example" in json_content .examples
410-
407+
411408 # Check XML content
412409 xml_content = response .content ["application/xml" ]
413410 assert xml_content .schema_ .type == "string"
414411 assert "xml_example" in xml_content .examples
415-
412+
416413 # Check plain text content
417414 text_content = response .content ["text/plain" ]
418415 assert text_content .schema_ .type == "string"
@@ -436,31 +433,31 @@ class RouterResponse(BaseModel):
436433 "X-Router-Header" : {
437434 "description" : "Header from router" ,
438435 "schema" : {"type" : "string" },
439- }
436+ },
440437 },
441438 "content" : {
442439 "application/json" : {
443440 "model" : RouterResponse ,
444441 "examples" : {
445442 "router_example" : {"value" : {"result" : "from_router" }},
446443 },
447- }
444+ },
448445 },
449- }
446+ },
450447 },
451448 )
452449 def router_handler () -> RouterResponse :
453450 return RouterResponse (result = "test" )
454451
455452 app .include_router (router )
456453 schema = app .get_openapi_schema ()
457-
454+
458455 response = schema .paths ["/router-test" ].get .responses [200 ]
459-
456+
460457 # Verify headers
461458 assert "X-Router-Header" in response .headers
462-
459+
463460 # Verify content with model and examples
464461 content = response .content ["application/json" ]
465462 assert content .schema_ .ref == "#/components/schemas/RouterResponse"
466- assert "router_example" in content .examples
463+ assert "router_example" in content .examples
0 commit comments