@@ -24,6 +24,11 @@ class NestedModel(BaseModel):
2424 extra : str | None = None
2525
2626
27+ class NestedListModel (BaseModel ):
28+ data : list [TestModel ]
29+ extra : str | None = None
30+
31+
2732class TestBaseRouter :
2833
2934 def setup_method (self ):
@@ -246,16 +251,65 @@ def create_user(user: TestModel):
246251 assert ref == "#/components/schemas/TestModel"
247252 assert "TestModel" in schema ["components" ]["schemas" ]
248253
254+ def test_generate_openapi_with_response_error (self ):
255+ # Test OpenAPI generation with response error
256+
257+ @self .router .post ("/users" , response_errors = [400 , 404 , 500 ], response_model = int )
258+ def create_user (user : TestModel ):
259+ return 1
260+
261+ schema = self .router .generate_openapi ()
262+
263+ # Check response
264+ response = schema ["paths" ]["/users" ]["post" ]["responses" ]["200" ]
265+ assert "content" in response
266+ assert "application/json" in response ["content" ]
267+
268+ responses = schema ["paths" ]["/users" ]["post" ]["responses" ]
269+ assert "200" in responses
270+ assert "400" in responses
271+ assert "404" in responses
272+ assert "500" in responses
273+
274+ assert "ErrorSchema" in schema ["components" ]["schemas" ]
275+
249276 def test_generate_openapi_with_incorrect_response_model (self ):
250277 # Test OpenAPI generation with incorrect response model
251278
252- @self .router .post ("/users" , response_model = int )
279+ @self .router .post ("/users" , response_model = tuple )
253280 def create_user (user : TestModel ):
254- return 1
281+ return ( 1 , 2 )
255282
256283 with pytest .raises (Exception , match = "Incorrect response_model" ):
257284 self .router .generate_openapi ()
258285
286+ def test_generate_openapi_with_incorrect_list_response_model (self ):
287+ # Test OpenAPI generation with incorrect response model
288+
289+ @self .router .post ("/users" , response_model = list [tuple ])
290+ def create_user (user : TestModel ):
291+ return [(1 , 2 )]
292+
293+ with pytest .raises (Exception , match = "Incorrect response_model" ):
294+ self .router .generate_openapi ()
295+
296+ def test_generate_openapi_with_simple_type_as_response_model (self ):
297+ # Test OpenAPI generation with python type as response model
298+
299+ @self .router .post ("/users" , response_model = int )
300+ def create_user (user : TestModel ):
301+ return 1
302+
303+ schema = self .router .generate_openapi ()
304+
305+ # Check response
306+ response = schema ["paths" ]["/users" ]["post" ]["responses" ]["200" ]
307+ assert "content" in response
308+ assert "application/json" in response ["content" ]
309+
310+ resp_type = response ["content" ]["application/json" ]["schema" ]["type" ]
311+ assert "integer" == resp_type
312+
259313 def test_generate_openapi_with_response_model (self ):
260314 # Test OpenAPI generation with response model
261315
@@ -276,6 +330,32 @@ def create_user(user: TestModel):
276330 assert ref == "#/components/schemas/ResponseModel"
277331 assert "ResponseModel" in schema ["components" ]["schemas" ]
278332
333+ def test_generate_openapi_with_nested_list_response_model (self ):
334+ # Test OpenAPI generation with response model with nested model list
335+
336+ @self .router .post ("/users" , response_model = NestedListModel )
337+ def create_user (user : TestModel ):
338+ pass
339+
340+ schema = self .router .generate_openapi ()
341+
342+ # Check response
343+ response = schema ["paths" ]["/users" ]["post" ]["responses" ]["200" ]
344+ assert "content" in response
345+ assert "application/json" in response ["content" ]
346+ assert "$ref" in response ["content" ]["application/json" ]["schema" ]
347+
348+ # Check that response model schema is in components
349+ ref = response ["content" ]["application/json" ]["schema" ]["$ref" ]
350+ schemas = schema ["components" ]["schemas" ]
351+ assert ref == "#/components/schemas/NestedListModel"
352+ assert "NestedListModel" in schemas
353+ assert "TestModel" in schemas
354+
355+ # Check that TestModel inside NestedModel
356+ ref = schemas ["NestedListModel" ]["properties" ]["data" ]["items" ]["$ref" ]
357+ assert ref == "#/components/schemas/TestModel"
358+
279359 def test_generate_openapi_with_list_response (self ):
280360 # Test OpenAPI generation with List response model
281361
0 commit comments