Skip to content

Commit 1ca98c6

Browse files
thejamesmarqalvarolopez
authored andcommitted
Add add_specs to api
This flag allows to select if specs are added into the API or not. Fixes #464
1 parent ddc2aee commit 1ca98c6

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Current
77
-------
88

99
- Ensure that exceptions raised in error handler, including programming errors, are logged (:issue:`705`, :pr:`706`)
10+
- Allow to select if specs are added or hidden in the API (:pr:`553`, :issue:`464`)
1011

1112
0.13.0 (2019-08-12)
1213
-------------------

doc/swagger.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,20 @@ You can hide some resources or methods from documentation using any of the follo
751751

752752
Namespace tags without attached resources will be hidden automatically from the documentation.
753753

754+
It is also possible to hide all the documentation or specs creating the API object like this:
755+
756+
.. code-block:: python
757+
758+
from flask import Flask
759+
from flask_restplus import Api
760+
761+
# Hide docs (default is '/', meaning that documentation will hang under '/')
762+
app = Flask(__name__)
763+
api = Api(app, doc=False)
764+
765+
# Hide specs (default is True, meaning that specs will be generated)
766+
api = Api(app, add_specs=False)
767+
754768
755769
Documenting authorizations
756770
--------------------------

flask_restplus/api.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class Api(object):
8787
:param FormatChecker format_checker: A jsonschema.FormatChecker object that is hooked into
8888
the Model validator. A default or a custom FormatChecker can be provided (e.g., with custom
8989
checkers), otherwise the default action is to not enforce any format validation.
90+
:param bool add_specs: Whether to add specs to the API or disable the specs generation
91+
(default 'True')
9092
'''
9193

9294
def __init__(self, app=None, version='1.0', title=None, description=None,
@@ -97,7 +99,7 @@ def __init__(self, app=None, version='1.0', title=None, description=None,
9799
tags=None, prefix='', ordered=False,
98100
default_mediatype='application/json', decorators=None,
99101
catch_all_404s=False, serve_challenge_on_401=False, format_checker=None,
100-
**kwargs):
102+
add_specs=True, **kwargs):
101103
self.version = version
102104
self.title = title or 'API'
103105
self.description = description
@@ -149,7 +151,7 @@ def __init__(self, app=None, version='1.0', title=None, description=None,
149151

150152
if app is not None:
151153
self.app = app
152-
self.init_app(app)
154+
self.init_app(app, add_specs=add_specs)
153155
# super(Api, self).__init__(app, **kwargs)
154156

155157
def init_app(self, app, **kwargs):

tests/test_api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ def test_specs_endpoint_not_found_if_not_added(self, app, client):
9595
resp = client.get('/swagger.json')
9696
assert resp.status_code == 404
9797

98+
def test_specs_endpoint_not_found_if_not_enabled(self, app, client):
99+
restplus.Api(app, add_specs=False)
100+
resp = client.get('/swagger.json')
101+
assert resp.status_code == 404
102+
98103
def test_default_endpoint(self, app):
99104
api = restplus.Api(app)
100105

0 commit comments

Comments
 (0)