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

Add option to exclude OPTIONS requests #123

Merged
merged 3 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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