-
-
Notifications
You must be signed in to change notification settings - Fork 161
Description
Hi team,
Currently, we provide two options to pass the parsed arguments to the view function:
use_argsuse_kwargs
It seems there is a mismatch here. use_args passes the parsed arguments as a single positional argument, while the use_kwargs passes all the arguments as separate keyword arguments.
I'm thinking of adding the missing feature between this mismatch, which is to support passing the whole parsed arguments as a keyword argument.
What are the benefits? It can help to ensure the natural order for Flask views:
@app.post('/pets/<pet_id>')
@use_args(PetQueryIn, location='json')
@use_args(PetBodyIn, location='query')
def create_pet(json_data, query_data, pet_id):
passSince the URL variables (i.e. pet_id) will be passed as keyword arguments, we have to declare the arguments from use_args first.
Although we can use use_kwargs to pass all arguments as keyword arguments, but consider we will use three stacked use_kwargs and each schema contains five fields...
I made some experiments, there are two ways to achieve this:
- Create a new function to pass the whole parsed arguments as a keyword argument (maybe called
use_named_args):
@app.post('/pets/<pet_id>')
@use_named_args(PetQueryIn, location='json', arg_name='data')
@use_named_args(PetBodyIn, location='query', arg_name='query_data')
def create_pet(pet_id, data, query_data):
pass- Add an
arg_nameargument touse_argsanduse_kwargs.
@app.post('/pets/<pet_id>')
@use_kwargs(PetQueryIn, location='json', arg_name='json_data')
@use_kwargs(PetBodyIn, location='query', arg_name='query_data')
def create_pet(json_data, query_data, pet_id):
passDo you think it's a useful feature? Which solution is preferred?
I'm happy to submit a PR. Thanks!
Related issue: apiflask/apiflask#427