-
-
Notifications
You must be signed in to change notification settings - Fork 140
Description
I recently bumped into an issue trying to convert an existing flask project to use apiflask. One of my view functions has a decorator which relies on arguments being passed as keyword arguments (kwargs) to work correctly. It appears apiflask does not pass arguments the same way that flask does when calling view functions. This seems to be by design for some reason, but it can break existing code.
At any rate, there are cases (as above) where the view function or its decorators needs the arguments to be in kwargs.
I found this piece of code in apiflask which will send kwargs (as flask does) if an attribute is set:
if rule.endpoint == 'static' or hasattr(view_function, '_only_kwargs'):
# app static route only accepts keyword arguments, see flask#3762
# view classes created by Flask only accept keyword arguments
return view_function(**req.view_args) # type: ignore
else:
return view_function(*req.view_args.values()) # type: ignoreI'm currently using a decorator to help me mark my view function as needing kwargs:
def require_kwargs(f):
"""
Tag a view function as needing kwargs. This is for apiflask which only passes positional
arguments unless this attribute exists on the view function.
"""
f._only_kwargs = True
return fUsage:
@bp.route("/some/path/<my_variable>/", methods=("GET",))
@require_kwargs
@another_decorator_that_needs_kwargs
def my_view_function(my_variable):
passA quick search shows I'm not the only one hitting this issue #422 .
Please consider allowing flask-behavior when passing args in kwargs, either globally or on a route-by-route basis, otherwise adopting apiflask in existing projects will be difficult. I suggest making this a global setting to adhere to existing flask behavior.