From d6beb925448f97ab351a96d98589b09f8183e63d Mon Sep 17 00:00:00 2001 From: Joe Kaufeld Date: Thu, 29 Aug 2024 00:53:59 -0400 Subject: [PATCH] :bug: fix issue with hardcoded argument name --- spiderweb/middleware/pydantic.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/spiderweb/middleware/pydantic.py b/spiderweb/middleware/pydantic.py index 5258680..45bbf00 100644 --- a/spiderweb/middleware/pydantic.py +++ b/spiderweb/middleware/pydantic.py @@ -1,3 +1,4 @@ +import inspect from typing import get_type_hints from pydantic import BaseModel @@ -19,13 +20,15 @@ class PydanticMiddleware(SpiderwebMiddleware): if not request.method == "POST": return types = get_type_hints(request.handler) - if "request" not in types: - return - try: - data = types["request"].parse_obj(request.POST) - request.validated_data = data - except ValidationError as e: - return self.on_error(request, e) + # we don't know what the user named the request object, but + # 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: + data = types[request_arg_name].parse_obj(request.POST) + request.validated_data = data + except ValidationError as e: + return self.on_error(request, e) def on_error(self, request: Request, e: ValidationError): # Separated out into its own method so that it can be overridden