flask-restx
flask-restx copied to clipboard
Disable swagger.json (by config)
Refering to https://github.com/noirbizarre/flask-restplus/issues/464
Are there any plans to merge https://github.com/noirbizarre/flask-restplus/pull/465 ?
It would be awesome if it could be disabled by app config as well, since that seems to be the way most apps differ between development and production.
Maybe how flask-apispec does it would be a good way: RESTX_SWAGGER_URL
and RESTX_SWAGGER_UI_URL
to define the routes. When set to None
, the corresponding component is disabled.
not so clear, but I tested to config the add_specs=False
in init_app()
, both doc and swagger.json will be disabled.
link to api.py
functions -> def _register_specs()
and def _register_doc()
by self._add_specs
That seems rather pokish. A clean way to separately disable specs and docs via init_app() argument and configuration setting would be preferable.
+1 for merging an option to disable
i believe it is already there, right?
sample:
from flask_restx import Api
api = Api()
api.init_app(<blueprint-or-app>, add_specs=False)
You are correct. Note to future readers. When using blueprints, per the docs at the link below, Api(app) is used instead of api.init_app()
. add_specs
is not a param of Api(app), so it won't work to pass add_specs to Api(app)
It turns out there's alternative way to use Restx with blueprints (which is not included in the documentation). You can pass the blueprint (instead of app) directly to init_app like so: api.init_app(blueprint). Here's the solution that also gives you access to the add_specs
attribute when using blueprints:
bp = Blueprint("api", __name__)
api = Api()
api.init_app(bp, title="Directory API", version="0.1.0", add_specs=False)
Not documented here but should be: https://flask-restx.readthedocs.io/en/latest/scaling.html#use-with-blueprints
You can disable the UI via the optional doc=False
parameter:
api = Api(app, doc=False)
Source: https://flask-restplus.readthedocs.io/en/0.8.2/swaggerui.html
Not documented here but should be: https://flask-restx.readthedocs.io/en/latest/scaling.html#use-with-blueprints
Although api.init_app(..., add_specs=False)
works to this day, it is likely undocumented because its left in place for legacy reasons. The documented way to disable Swagger with current versions is to as zees-dev
mentioned: api = Api(app, doc=False)
.
The documented way to disable Swagger with current versions is to as zees-dev mentioned:
api = Api(app, doc=False)
.
⚠️ It is worth noting that even if you set doc=false
, the endpoint to serve the swagger.json stays in place. Requesting /api/swagger.json
(or /{api_prefix}/swagger.json
if you have a different prefix) will still return the swagger description of all of your endpoints.
Yikes! That is good to know. Maybe it should get documented...
So the proper way to completely hide the swagger APIs is to:
from flask_restx import Api
api = Api()
api.init_app(<blueprint-or-app>, add_specs=False, doc=False)