fix: keep __annotations__ in wrap_plugin_route — body params need ForwardRef resolution
Removing __annotations__ broke body parameter resolution: FastAPI could
not resolve ForwardRef('ConversationCreate') etc. causing 422 on all
POST routes with body params. Now keeping annotations from functools.wraps
and only removing return_annotation.
This commit is contained in:
@@ -3,7 +3,7 @@ import logging
|
|||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
from fastapi import UploadFile # noqa: F401 — needed for ForwardRef resolution
|
from fastapi import UploadFile # noqa: F401 — needed for ForwardRef resolution
|
||||||
from fastapi import WebSocket # noqa: F401 — needed for ForwardRef resolution of WebSocket params
|
from fastapi import WebSocket # noqa: F401 — needed for ForwardRef resolution
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -14,7 +14,8 @@ def wrap_plugin_route(handler):
|
|||||||
|
|
||||||
Copies the original handler's signature so FastAPI sees the correct
|
Copies the original handler's signature so FastAPI sees the correct
|
||||||
parameters (path params, query params, body, etc.) instead of *args/**kwargs.
|
parameters (path params, query params, body, etc.) instead of *args/**kwargs.
|
||||||
Removes return annotation to prevent ForwardRef('UploadFile') resolution issues.
|
UploadFile and WebSocket are imported in this module's namespace so
|
||||||
|
FastAPI can resolve ForwardRef('UploadFile') and ForwardRef('WebSocket').
|
||||||
"""
|
"""
|
||||||
@functools.wraps(handler)
|
@functools.wraps(handler)
|
||||||
async def wrapper(*args, **kwargs):
|
async def wrapper(*args, **kwargs):
|
||||||
@@ -26,18 +27,16 @@ def wrap_plugin_route(handler):
|
|||||||
status_code=500,
|
status_code=500,
|
||||||
content={'detail': f'Plugin error: {exc}', 'code': 'plugin_error'}
|
content={'detail': f'Plugin error: {exc}', 'code': 'plugin_error'}
|
||||||
)
|
)
|
||||||
# Remove annotations that cause ForwardRef resolution issues
|
|
||||||
wrapper.__annotations__ = {}
|
|
||||||
# Remove __wrapped__ so FastAPI doesn't try to resolve the original signature
|
# Remove __wrapped__ so FastAPI doesn't try to resolve the original signature
|
||||||
|
# through the wrapper chain — we set __signature__ explicitly instead.
|
||||||
if hasattr(wrapper, '__wrapped__'):
|
if hasattr(wrapper, '__wrapped__'):
|
||||||
delattr(wrapper, '__wrapped__')
|
delattr(wrapper, '__wrapped__')
|
||||||
# Copy the signature from the original handler but remove return annotation
|
# Copy the signature from the original handler so FastAPI sees correct params.
|
||||||
|
# Keep __annotations__ from functools.wraps (needed for ForwardRef resolution).
|
||||||
|
# Remove only the return annotation to avoid response_model issues.
|
||||||
try:
|
try:
|
||||||
orig_sig = inspect.signature(handler)
|
orig_sig = inspect.signature(handler)
|
||||||
# Create new signature without return annotation
|
|
||||||
new_params = list(orig_sig.parameters.values())
|
|
||||||
wrapper.__signature__ = orig_sig.replace(
|
wrapper.__signature__ = orig_sig.replace(
|
||||||
parameters=new_params,
|
|
||||||
return_annotation=inspect.Signature.empty,
|
return_annotation=inspect.Signature.empty,
|
||||||
)
|
)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
|
|||||||
Reference in New Issue
Block a user