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

Disable swagger.json (by config) #116

Open
heeplr opened this issue Apr 15, 2020 · 9 comments · May be fixed by #490 or #532
Open

Disable swagger.json (by config) #116

heeplr opened this issue Apr 15, 2020 · 9 comments · May be fixed by #490 or #532
Labels
enhancement New feature or request

Comments

@heeplr
Copy link

heeplr commented Apr 15, 2020

Refering to noirbizarre/flask-restplus#464

Are there any plans to merge noirbizarre/flask-restplus#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 heeplr added the enhancement New feature or request label Apr 15, 2020
@sttt24
Copy link

sttt24 commented Apr 18, 2020

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

@heeplr heeplr changed the title Disable swagger.json Disable swagger.json (by config) Apr 29, 2020
@heeplr
Copy link
Author

heeplr commented Apr 29, 2020

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

@kablamus
Copy link

kablamus commented Jun 5, 2020

+1 for merging an option to disable

@laminko
Copy link

laminko commented Jun 5, 2020

i believe it is already there, right?

sample:

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

@kablamus
Copy link

kablamus commented Jun 5, 2020

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

@zees-dev
Copy link

zees-dev commented Nov 5, 2020

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

@JavaScriptDude
Copy link

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

@plowman
Copy link
Contributor

plowman commented May 27, 2022

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.

@JavaScriptDude
Copy link

JavaScriptDude commented May 27, 2022

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)

@FwP-IDN FwP-IDN linked a pull request Nov 27, 2022 that will close this issue
@StellaContrail StellaContrail linked a pull request Apr 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
7 participants