🐛 fix issue with hardcoded argument name

This commit is contained in:
Joe Kaufeld 2024-08-29 00:53:59 -04:00
parent d4f8109663
commit d6beb92544

View File

@ -1,3 +1,4 @@
import inspect
from typing import get_type_hints from typing import get_type_hints
from pydantic import BaseModel from pydantic import BaseModel
@ -19,10 +20,12 @@ class PydanticMiddleware(SpiderwebMiddleware):
if not request.method == "POST": if not request.method == "POST":
return return
types = get_type_hints(request.handler) types = get_type_hints(request.handler)
if "request" not in types: # we don't know what the user named the request object, but
return # we know that it's first in the list, and it's always an arg.
request_arg_name = inspect.getfullargspec(request.handler).args[0]
if types.get(request_arg_name) in RequestModel.__subclasses__():
try: try:
data = types["request"].parse_obj(request.POST) data = types[request_arg_name].parse_obj(request.POST)
request.validated_data = data request.validated_data = data
except ValidationError as e: except ValidationError as e:
return self.on_error(request, e) return self.on_error(request, e)