I am writing flask backend application which uses flask-restful. I created swagger.yaml using swagger.io's online editor and exported its connexion based server implementation of swagger UI. I merged that code on top of my existing application and after that I tried to run my application.
It should be able to start the server and localhost:5000/v1/ui should render swagger UI.
Code is not compiling and failing while I am trying to use connexion flask instance with flask-restful.
Below is the stack trace:
Traceback (most recent call last):
File "app.py", line 17, in
api = Api(webapp)
File "C:\stuff\Git\museum_link_curation\flask-backend\lib\site-packages\flask_restful__init__.py", line 96, in init
self.init_app(app)
File "C:\stuff\Git\museum_link_curation\flask-backend\lib\site-packages\flask_restful__init__.py", line 118, in init_app
self._init_app(app)
File "C:\stuff\Git\museum_link_curation\flask-backend\lib\site-packages\flask_restful__init__.py", line 195, in _init_app
app.handle_exception = partial(self.error_router, app.handle_exception)
AttributeError: 'App' object has no attribute 'handle_exception'
Below is the code that I changed to merge connexion:
#webapp = Flask(__name__) # commented
webapp = connexion.App(__name__, specification_dir='swagger/') # newly added
webapp.add_api('swagger.yaml') #newly added
api = Api(webapp) #existing
...
if __name__ == '__main__': #existing
webapp.run(debug=True) #existing
Use connexion app instance for initialize flask-restful instead of flask instance.
Running on Windows 10 machine. Primarily on Python 2 with hacks mentioned from http://stackoverflow.com/questions/36416679/error-generating-swagger-server-python-flask-from-swagger-editor. However, tested on Python3 and same error is coming there as well.
Output of the commands:
python --versionpip show connexion | grep "^Version\:"
Name: connexion
Version: 1.0.91
Summary: Connexion - API first applications with OpenAPI/Swagger and Flask
Home-page: https://github.com/zalando/connexion
Author: Zalando SE
Author-email: UNKNOWN
Installer: pip
License: Apache License Version 2.0
Location: c:\stuff\git\museum_link_curation\flask-backend\lib\site-packages
Requires: requests, swagger-spec-validator, six, strict-rfc3339, PyYAML, flask, pathlib, jsonschema
Classifiers:
Programming Language :: Python
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3.4
Programming Language :: Python :: 3.5
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
Operating System :: OS Independent
Topic :: Internet :: WWW/HTTP :: WSGI :: Application
' Topic :: Software Development :: Libraries :: Application Frameworks'`I ran into a similar problem and the reason is that connexion.App is not a direct swap for Flask. connexion does however encapsulate the Flask instance in an app variable
If your original code is:
webapp = Flask(__name__)
api = Api(webapp)
Then the connexion equivalent is:
connexion_app = connexion.App(__name__)
connexion_app.add_api('swagger.yaml')
webapp = connexion_app.app
You do not need to create an API instance since connexion does that for you.
Disclaimer: This may lead to unexpected side affects, yet to be found, but so far it works for me.
@thirty3 You are correct.
@mit2nil Could you please let us know if that solves your issue?
Thanks @rafaelcaricio and @thirty3 . Yes this did solve the issue.
i also found the same issue, can you please help me to fix this. i already tried the above method.
Most helpful comment
I ran into a similar problem and the reason is that
connexion.Appis not a direct swap forFlask. connexion does however encapsulate the Flask instance in anappvariableIf your original code is:
Then the connexion equivalent is:
You do not need to create an
APIinstance since connexion does that for you.Disclaimer: This may lead to unexpected side affects, yet to be found, but so far it works for me.