1+ from typing import Union
2+ from unittest import TestCase
3+ from osbot_fast_api .client .Fast_API__Route__Extractor import Fast_API__Route__Extractor
4+ from osbot_utils .type_safe .Type_Safe import Type_Safe
5+ from osbot_fast_api .api .Fast_API import Fast_API
6+
7+
8+ class test_Fast_API__Routes__regression (TestCase ):
9+
10+ def test__regression__error_with_union_return_type (self ): # Test that Union return types are handled gracefully (no error, but return_type not set)
11+
12+ def an_union_return_type () -> Union [str , int ]:
13+ return "test"
14+
15+ with Fast_API () as fast_api :
16+ fast_api .add_route_get (an_union_return_type ) # Should not raise an error anymore
17+
18+ routes = fast_api .routes () # Verify the route was added successfully
19+ assert len (routes ) > 0
20+
21+ our_route = None # Find our route
22+ for route in routes :
23+ if route .get ('method_name' ) == 'an_union_return_type' :
24+ our_route = route
25+ break
26+
27+ assert our_route is not None , "Route should be registered"
28+
29+ # Verify that return_type is None (Union types are skipped)
30+ assert our_route .get ('return_type' ) is None , "Union return types should be skipped, resulting in None return_type"
31+
32+ client = fast_api .client () # Verify the route still works
33+ response = client .get ('/an-union-return-type' )
34+ assert response .status_code == 200
35+
36+ def test__regression__concrete_return_type_works (self ): # Test that concrete return types still work correctly
37+
38+ class Schema__Response (Type_Safe ):
39+ message : str
40+
41+ def concrete_return_type () -> Schema__Response :
42+ return Schema__Response (message = "test" )
43+
44+ with Fast_API () as fast_api :
45+ fast_api .add_route_get (concrete_return_type )
46+
47+ extractor = Fast_API__Route__Extractor (app = fast_api .app (), include_default = False ) # Use Fast_API__Route__Extractor to get full route schemas
48+ routes_collection = extractor .extract_routes ()
49+
50+ our_route = None # Find our route
51+ for route in routes_collection .routes :
52+ if route .method_name == 'concrete_return_type' :
53+ our_route = route
54+ break
55+
56+ assert our_route is not None , "Route should be registered"
57+
58+ assert our_route .http_path == '/concrete-return-type' # Verify the route structure
59+ assert 'GET' in [m .value for m in our_route .http_methods ]
60+
61+ assert our_route .return_type is not None , "Concrete return types should be captured" # Verify that return_type IS set for concrete types
62+ assert our_route .return_type == Schema__Response , "Return type should match the Type_Safe class"
63+
64+ client = fast_api .client () # Verify the route works
65+ response = client .get ('/concrete-return-type' )
66+ assert response .status_code == 200
67+ assert response .json () == {"message" : "test" }
0 commit comments