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.

Question: multiple @response decorators

See original GitHub issue

Hello, i have defined two response types on my view function:

    @blp.response(code=404, description="Product not found")
    @blp.response(ProductSchema(), code=200)
    def get(self, product_id):
        ...

Generated documentations contains description of those two possible response types and looks perfect.

If view is aborted via abort(404) then everything works as expected, but i got an error otherwise:

2018-11-22 18:18:43.686 INFO    127.0.0.1 - - [22/Nov/2018 18:18:43] "GET /api/products/1/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/views.py", line 88, in view
    return self.dispatch_request(*args, **kwargs)
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/views.py", line 158, in dispatch_request
    return meth(*args, **kwargs)
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask_rest_api/response.py", line 55, in wrapper
    resp = jsonify(self._prepare_response_content(result_dump))
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/json/__init__.py", line 321, in jsonify
    dumps(data, indent=indent, separators=separators) + '\n',
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/json/__init__.py", line 179, in dumps
    rv = _json.dumps(obj, **kwargs)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 437, in _iterencode
    o = _default(o)
  File "/Users/asyncee/app/env/lib/python3.6/site-packages/flask/json/__init__.py", line 81, in default
    return _json.JSONEncoder.default(self, o)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default
    o.__class__.__name__)
TypeError: Object of type 'Response' is not JSON serializable

So my question is: am i doing everything correctly and multiple decorators supported?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
erdnaxelicommented, Jan 6, 2020

This “only one response per resource” thing should be documented, it’s not really intuitive.

2reactions
revmischacommented, May 13, 2019

It would be nice if it was easy to document errors (e.g. 403) using multiple @blp.response

Read more comments on GitHub >

github_iconTop Results From Across the Web

A decorator that returns multiple functions - python
The only problem I have here is that globals gets the symbol table for the module where the decorator is defined, not where...
Read more >
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 >
Python Questions and Answers – Decorators - Sanfoundry
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Decorators”. 1. What will be the output of the following Python...
Read more >
Primer on Python Decorators
In this introductory tutorial, we'll look at what Python decorators are and how to create and use them.
Read more >
17 Event Decorator Interview Questions and Answers - CLIMB
Event decorators often work on multiple projects at once, so employers ask this question to make sure you can manage your time well...
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