question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decorate with multiple/chained parameters

See original GitHub issue

I would like to add multiple parameters to an endpoint, essentially chaining them as so:

 @api.parameters(SomeParameters())
 @api.parameters(MoreParameters())
 @api.parameters(EvenMoreParameters())
 def get(self, args):
    ...

However, this results in a separate dict for each set of parameters and does not concatenate the arguments as I had expected. Also, Swagger does not seem to parse all of the argument options as only the topmost decorator appears in the docs.

image

I could simply create another Parameters class that combines all of the fields from the others, but I’d like to avoid having to create one for each desired combination of parameters.

Another idea I had was to create a method that would accept any number of Parameter objects and return a new dynamic Schema object with all of the combined fields. Something like:

 @api.parameters(many_params(SomeParameters(), MoreParameters(), EvenMoreParameters()))
 def get(self, args):
    ...

What is the recommended approach to accomplish this? I feel like I must be missing an easier way…

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jdjodreycommented, Feb 2, 2017

Here’s what I ended up going with:

def multi_params(*params):
    m = Parameters()
    for p in params:
        m.fields = dict(m.fields, **p.fields)
    return m

And then you can simply put that callable in the decorate and pass in any number of parameter objects to it:

@api.parameters(multi_params(IncludeParameters(), OnlyParameters()))

It’s somewhat crude and is definitely limited in handling more complex objects, but it serves my purpose for simple parameters with one or two fields (and it keeps Swagger happy too).

For anyone looking to do something similar, please feel free to improve/refine it!

1reaction
soundstripecommented, Dec 18, 2017

I’m running across the same problem here. I’d like to add that it would be nice to be able to specify different locations for each set of parameters. Something like:

@api.parameters(AuthenticationParameters(), locations['header', ])
@api.parameters(PaginationParameters(), locations['query', ])
@api.parameters(SearchQueryParameters(), locations=['json', ])
def get():
    return {}

If I come up with anything I’ll drop it here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chain Multiple Decorators in Python - GeeksforGeeks
Chaining decorators means applying more than one decorator inside a function. Python allows us to implement more than one decorator to a ...
Read more >
How to make function decorators and chain them together?
Although this answer has the great advantage of only using the stdlib, and works for this simple example where there are no decorator...
Read more >
Advanced Lesson 4: Python Decorators > Chaining decorators
You can also decorate a function with multiple decorators. They will be executed in the order that they are listed. What do you...
Read more >
Python Decorators: From Simple Decorators to Nesting Multiple
In this post, we will discuss a few and how multiple decorators can be chained together to truly enhance the functionality of an...
Read more >
Chaining Decorators, Python Pie Syntax - DataFlair
If you don't want to type in the whole list of arguments for the two statements, *args and **kwargs will do the trick...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found