Skip to content

Commit 1296885

Browse files
authored
Merge pull request #123 from Brcrwilliams/hide-options
Add option to exclude OPTIONS requests
2 parents ec30f52 + 7a27abf commit 1296885

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

CHANGELOG.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Changelog
22
---------
33

4+
0.9.0 (unreleased)
5+
******************
6+
7+
Features:
8+
9+
* Add option to exclude OPTIONS requests (:issue:`111`).
10+
Thanks :user:`Brcrwilliams` for the PR.
11+
412
0.8.8 (2020-03-29)
513
******************
614

flask_apispec/apidoc.py

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

2121
class Converter(object):
2222

23-
def __init__(self, app, spec):
23+
def __init__(self, app, spec, document_options=True):
2424
self.app = app
2525
self.spec = spec
26+
self.document_options = document_options
2627
try:
2728
self.marshmallow_plugin = next(
2829
plugin for plugin in self.spec.plugins
@@ -45,13 +46,16 @@ def get_path(self, rule, target, **kwargs):
4546
operations = self.get_operations(rule, target)
4647
parent = self.get_parent(target, **kwargs)
4748
valid_methods = VALID_METHODS[self.spec.openapi_version.major]
49+
excluded_methods = {'head'}
50+
if not self.document_options:
51+
excluded_methods.add('options')
4852
return {
4953
'view': target,
5054
'path': rule_to_path(rule),
5155
'operations': {
5256
method.lower(): self.get_operation(rule, view, parent=parent)
5357
for method, view in six.iteritems(operations)
54-
if method.lower() in (set(valid_methods) - {'head'})
58+
if method.lower() in (set(valid_methods) - excluded_methods)
5559
},
5660
}
5761

flask_apispec/extension.py

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

41-
def __init__(self, app=None):
43+
def __init__(self, app=None, document_options=True):
4244
self._deferred = []
4345
self.app = app
4446
self.view_converter = None
4547
self.resource_converter = None
4648
self.spec = None
49+
self.document_options = document_options
4750

4851
if app:
4952
self.init_app(app)
@@ -55,8 +58,10 @@ def init_app(self, app):
5558
self.app.config.get('APISPEC_VERSION', 'v1'),
5659
self.app.config.get('APISPEC_OAS_VERSION', '2.0'))
5760
self.add_swagger_routes()
58-
self.resource_converter = ResourceConverter(self.app, spec=self.spec)
59-
self.view_converter = ViewConverter(app=self.app, spec=self.spec)
61+
self.resource_converter = ResourceConverter(self.app,
62+
self.spec,
63+
self.document_options)
64+
self.view_converter = ViewConverter(self.app, self.spec, self.document_options)
6065

6166
for deferred in self._deferred:
6267
deferred()

0 commit comments

Comments
 (0)