flask-restx icon indicating copy to clipboard operation
flask-restx copied to clipboard

Disable swagger.json (by config)

Open heeplr opened this issue 4 years ago • 9 comments

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.

heeplr avatar Apr 15 '20 20:04 heeplr

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

sttt24 avatar Apr 18 '20 14:04 sttt24

That seems rather pokish. A clean way to separately disable specs and docs via init_app() argument and configuration setting would be preferable.

heeplr avatar Apr 29 '20 09:04 heeplr

+1 for merging an option to disable

kablamus avatar Jun 05 '20 01:06 kablamus

i believe it is already there, right?

sample:

from flask_restx import Api
api = Api()
api.init_app(<blueprint-or-app>, add_specs=False)

laminko avatar Jun 05 '20 03:06 laminko

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

kablamus avatar Jun 05 '20 03:06 kablamus

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

zees-dev avatar Nov 05 '20 04:11 zees-dev

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).

JavaScriptDude avatar May 13 '22 17:05 JavaScriptDude

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.

plowman avatar May 27 '22 18:05 plowman

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)

JavaScriptDude avatar May 27 '22 18:05 JavaScriptDude