11import functools
22import inspect
3- from typing import Callable
3+ from typing import Callable , get_type_hints
44from fastapi import HTTPException
55from fastapi .exceptions import RequestValidationError
66from osbot_utils .type_safe .Type_Safe import Type_Safe
@@ -18,7 +18,9 @@ def create_wrapper(self, function : Callable , # Origin
1818 ) -> Callable : # Returns wrapper function
1919
2020 if not signature .primitive_conversions and not signature .type_safe_conversions and not signature .return_needs_conversion :
21- return function # No conversion needed - return original
21+ if signature .return_type is not None : # Even if no conversions needed, preserve return type for OpenAPI
22+ return self .create_passthrough_wrapper (function , signature ) # Create minimal wrapper that preserves return type annotation
23+ return function # No return type - return original
2224
2325 if signature .has_body_params : # Different wrappers for different scenarios
2426 wrapper_function = self .create_body_wrapper (function , signature )
@@ -39,6 +41,33 @@ def create_wrapper(self, function : Callable , # Origin
3941
4042 return wrapper_function
4143
44+ @type_safe
45+ def create_passthrough_wrapper (self , function : Callable , # Function to wrap
46+ signature : Schema__Route__Signature # Signature info
47+ ) -> Callable : # Returns minimal wrapper that preserves annotations
48+
49+ @functools .wraps (function )
50+ def wrapper (* args , ** kwargs ):
51+ return function (* args , ** kwargs ) # Simply pass through to the original function
52+
53+ wrapper .__signature__ = inspect .signature (function ) # Preserve the original signature
54+
55+ try : # Build annotations dict including the return type
56+ type_hints = get_type_hints (function )
57+ wrapper .__annotations__ = type_hints .copy ()
58+ except :
59+ wrapper .__annotations__ = getattr (function , '__annotations__' , {}).copy () # Fallback to __annotations__ if get_type_hints fails
60+
61+ if signature .return_type is not None and 'return' not in wrapper .__annotations__ : # Ensure return type is set
62+ wrapper .__annotations__ ['return' ] = signature .return_type
63+
64+ wrapper .__original_return_type__ = signature .return_type # Preserve original return type metadata
65+
66+ if hasattr (function , '__route_path__' ): # Preserve route_path decorator if it exists
67+ wrapper .__route_path__ = function .__route_path__
68+
69+ return wrapper
70+
4271 @type_safe
4372 def create_body_wrapper (self , function : Callable , # Function to wrap
4473 signature : Schema__Route__Signature # Signature info
0 commit comments