diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 244812f..b958656 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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) ****************** diff --git a/flask_apispec/apidoc.py b/flask_apispec/apidoc.py index fd49242..506c5a1 100644 --- a/flask_apispec/apidoc.py +++ b/flask_apispec/apidoc.py @@ -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 @@ -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) }, } diff --git a/flask_apispec/extension.py b/flask_apispec/extension.py index 68636b1..81404ed 100644 --- a/flask_apispec/extension.py +++ b/flask_apispec/extension.py @@ -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) @@ -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()