Skip to content

Commit

Permalink
Merge pull request #123 from Brcrwilliams/hide-options
Browse files Browse the repository at this point in the history
Add option to exclude OPTIONS requests
  • Loading branch information
sloria authored May 25, 2020
2 parents ec30f52 + 7a27abf commit 1296885
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
---------

0.9.0 (unreleased)
******************

Features:

* Add option to exclude OPTIONS requests (:issue:`111`).
Thanks :user:`Brcrwilliams` for the PR.

0.8.8 (2020-03-29)
******************

Expand Down
8 changes: 6 additions & 2 deletions flask_apispec/apidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

class Converter(object):

def __init__(self, app, spec):
def __init__(self, app, spec, document_options=True):
self.app = app
self.spec = spec
self.document_options = document_options
try:
self.marshmallow_plugin = next(
plugin for plugin in self.spec.plugins
Expand All @@ -45,13 +46,16 @@ def get_path(self, rule, target, **kwargs):
operations = self.get_operations(rule, target)
parent = self.get_parent(target, **kwargs)
valid_methods = VALID_METHODS[self.spec.openapi_version.major]
excluded_methods = {'head'}
if not self.document_options:
excluded_methods.add('options')
return {
'view': target,
'path': rule_to_path(rule),
'operations': {
method.lower(): self.get_operation(rule, view, parent=parent)
for method, view in six.iteritems(operations)
if method.lower() in (set(valid_methods) - {'head'})
if method.lower() in (set(valid_methods) - excluded_methods)
},
}

Expand Down
11 changes: 8 additions & 3 deletions flask_apispec/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ def get_pet(pet_id):
:param Flask app: App associated with API documentation
:param APISpec spec: apispec specification associated with API documentation
:param bool document_options: Whether or not to include
OPTIONS requests in the specification
"""

def __init__(self, app=None):
def __init__(self, app=None, document_options=True):
self._deferred = []
self.app = app
self.view_converter = None
self.resource_converter = None
self.spec = None
self.document_options = document_options

if app:
self.init_app(app)
Expand All @@ -55,8 +58,10 @@ def init_app(self, app):
self.app.config.get('APISPEC_VERSION', 'v1'),
self.app.config.get('APISPEC_OAS_VERSION', '2.0'))
self.add_swagger_routes()
self.resource_converter = ResourceConverter(self.app, spec=self.spec)
self.view_converter = ViewConverter(app=self.app, spec=self.spec)
self.resource_converter = ResourceConverter(self.app,
self.spec,
self.document_options)
self.view_converter = ViewConverter(self.app, self.spec, self.document_options)

for deferred in self._deferred:
deferred()
Expand Down

0 comments on commit 1296885

Please sign in to comment.