🐛 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,13 +20,15 @@ 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.
try: request_arg_name = inspect.getfullargspec(request.handler).args[0]
data = types["request"].parse_obj(request.POST) if types.get(request_arg_name) in RequestModel.__subclasses__():
request.validated_data = data try:
except ValidationError as e: data = types[request_arg_name].parse_obj(request.POST)
return self.on_error(request, e) request.validated_data = data
except ValidationError as e:
return self.on_error(request, e)
def on_error(self, request: Request, e: ValidationError): def on_error(self, request: Request, e: ValidationError):
# Separated out into its own method so that it can be overridden # Separated out into its own method so that it can be overridden