Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cause AttributeError when use flask_debugtoolbar #344

Closed
hjlarry opened this issue Aug 23, 2022 · 1 comment · Fixed by #375
Closed

cause AttributeError when use flask_debugtoolbar #344

hjlarry opened this issue Aug 23, 2022 · 1 comment · Fixed by #375
Assignees
Labels
bug Something isn't working
Milestone

Comments

@hjlarry
Copy link
Contributor

hjlarry commented Aug 23, 2022

When start a new project, and install the flask_debugtoolbar plugin, then visit http://127.0.0.1:5000/openapi.json will cause the error:

127.0.0.1 - - [23/Aug/2022 16:50:22] "GET /openapi.json HTTP/1.1" 500 -
Traceback (most recent call last):
  File "c:\users\hejl\pycharmprojects\apiflask_example\venv\lib\site-packages\flask\app.py", line 2095, in __call__
    return self.wsgi_app(environ, start_response)
  File "c:\users\hejl\pycharmprojects\apiflask_example\venv\lib\site-packages\flask\app.py", line 2080, in wsgi_app
    response = self.handle_exception(e)
  File "c:\users\hejl\pycharmprojects\apiflask_example\venv\lib\site-packages\flask\app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\users\hejl\pycharmprojects\apiflask_example\venv\lib\site-packages\flask\app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\users\hejl\pycharmprojects\apiflask_example\venv\lib\site-packages\flask\app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\users\hejl\pycharmprojects\apiflask_example\venv\lib\site-packages\flask_debugtoolbar\__init__.py", line 142, in dispatch_request
    return view_func(**req.view_args)
  File "c:\users\hejl\pycharmprojects\apiflask_example\venv\lib\site-packages\apiflask\app.py", line 583, in spec
    response = jsonify(self._get_spec('json'))
  File "c:\users\hejl\pycharmprojects\apiflask_example\venv\lib\site-packages\apiflask\app.py", line 668, in _get_spec
    self._spec = self._generate_spec().to_dict()
  File "c:\users\hejl\pycharmprojects\apiflask_example\venv\lib\site-packages\apiflask\app.py", line 909, in _generate_spec
    view_func._spec = {'response': default_response}
AttributeError: 'method' object has no attribute '_spec'

That is because flask_debugtoolbar's init_app() method has add a route this way:

        app.add_url_rule('/_debug_toolbar/static/<path:filename>',
                         '_debug_toolbar.static', self.send_static_file)

apiflask process the route this way:

            if not hasattr(view_func, '_spec'):
                if self.config['AUTO_200_RESPONSE']:
                    view_func._spec = {'response': default_response}
                else:
                    continue  # pragma: no cover

the view_func here is a boundmethod: DebugToolbarExtension.send_static_file(), so we can't set attribute to a method.
I think we can fix it this way:

            if not hasattr(view_func, '_spec'):
                if not self.config['AUTO_200_RESPONSE'] or inspect.ismethod(view_func):
                    continue  # pragma: no cover
                view_func._spec = {'response': default_response}

Environment:

  • Python version: 3.10.5
  • Flask version: 2.1.0
  • APIFlask version: 1.1.2
  • flask_debugtoolbar version: 0.13.1
@hjlarry hjlarry added the bug Something isn't working label Aug 23, 2022
@greyli greyli added this to the 1.1.3 milestone Aug 23, 2022
@greyli
Copy link
Member

greyli commented Aug 26, 2022

The proposed fix looks good to me, feel free to make a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants