Hi team,
Currently, we provide two options to pass the parsed arguments to the view function:
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):
pass
Since 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_name argument to use_args and use_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):
pass
Do you think it's a useful feature? Which solution is preferred?
I'm happy to submit a PR. Thanks!
Related issue: apiflask/apiflask#427
Hi team,
Currently, we provide two options to pass the parsed arguments to the view function:
use_argsuse_kwargsIt seems there is a mismatch here.
use_argspasses the parsed arguments as a single positional argument, while theuse_kwargspasses 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:
Since 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_kwargsto pass all arguments as keyword arguments, but consider we will use three stackeduse_kwargsand each schema contains five fields...I made some experiments, there are two ways to achieve this:
use_named_args):arg_nameargument touse_argsanduse_kwargs.Do you think it's a useful feature? Which solution is preferred?
I'm happy to submit a PR. Thanks!
Related issue: apiflask/apiflask#427