Skip to content

Commit 203800d

Browse files
committed
Add option to exclude OPTIONS requests
These changes add a new optional parameter called `document_options` to the FlaskApiSpec constructor that will exclude OPTIONS requests from the swagger specification if set to `False`. Flask automatically generates OPTIONS requests for each route. There are cases, such as when using CORS, where one would want these OPTIONS requests to be generated but do not want to have them in their swagger docs. My line of reasoning for wanting to exclude these is: My API users will never explicitly make an OPTIONS request. The browser will automatically send a pre-flight OPTIONS request when making a cross-origin request, and I want to have OPTIONS endpoints in order to support that, but I don't want to have this functionality adding a bunch of endpoints to my swagger docs which will largely be ignored by users.
1 parent 535fc9d commit 203800d

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

flask_apispec/apidoc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515

1616
class Converter(object):
1717

18-
def __init__(self, app, spec):
18+
def __init__(self, app, spec, document_options=True):
1919
self.app = app
2020
self.spec = spec
21+
self.document_options = document_options
2122
try:
2223
self.marshmallow_plugin = next(
2324
plugin for plugin in self.spec.plugins
@@ -39,13 +40,16 @@ def convert(self, target, endpoint=None, blueprint=None, **kwargs):
3940
def get_path(self, rule, target, **kwargs):
4041
operations = self.get_operations(rule, target)
4142
parent = self.get_parent(target, **kwargs)
43+
excluded_methods = {'head'}
44+
if not self.document_options:
45+
excluded_methods.add('options')
4246
return {
4347
'view': target,
4448
'path': rule_to_path(rule),
4549
'operations': {
4650
method.lower(): self.get_operation(rule, view, parent=parent)
4751
for method, view in six.iteritems(operations)
48-
if method.lower() in (set(VALID_METHODS) - {'head'})
52+
if method.lower() in (set(VALID_METHODS) - excluded_methods)
4953
},
5054
}
5155

flask_apispec/extension.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ def get_pet(pet_id):
3535
3636
:param Flask app: App associated with API documentation
3737
:param APISpec spec: apispec specification associated with API documentation
38+
:param bool document_options: Whether or not to include
39+
OPTIONS requests in the specification
3840
"""
3941

40-
def __init__(self, app=None):
42+
def __init__(self, app=None, document_options=True):
4143
self._deferred = []
4244
self.app = app
4345
self.view_converter = None
4446
self.resource_converter = None
4547
self.spec = None
48+
self.document_options = document_options
4649

4750
if app:
4851
self.init_app(app)
@@ -53,8 +56,10 @@ def init_app(self, app):
5356
make_apispec(self.app.config.get('APISPEC_TITLE', 'flask-apispec'),
5457
self.app.config.get('APISPEC_VERSION', 'v1'))
5558
self.add_swagger_routes()
56-
self.resource_converter = ResourceConverter(self.app, spec=self.spec)
57-
self.view_converter = ViewConverter(app=self.app, spec=self.spec)
59+
self.resource_converter = ResourceConverter(self.app,
60+
self.spec,
61+
self.document_options)
62+
self.view_converter = ViewConverter(self.app, self.spec, self.document_options)
5863

5964
for deferred in self._deferred:
6065
deferred()

0 commit comments

Comments
 (0)