From 203800d7971095d577de29167ec8b22c6f11cc6c Mon Sep 17 00:00:00 2001 From: Brian Williams Date: Sat, 20 Oct 2018 20:49:28 -0500 Subject: [PATCH 1/2] 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. --- flask_apispec/apidoc.py | 8 ++++++-- flask_apispec/extension.py | 11 ++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/flask_apispec/apidoc.py b/flask_apispec/apidoc.py index 239c673..4e10fed 100644 --- a/flask_apispec/apidoc.py +++ b/flask_apispec/apidoc.py @@ -15,9 +15,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 @@ -39,13 +40,16 @@ def convert(self, target, endpoint=None, blueprint=None, **kwargs): def get_path(self, rule, target, **kwargs): operations = self.get_operations(rule, target) parent = self.get_parent(target, **kwargs) + 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 2c6bbb5..4a45577 100644 --- a/flask_apispec/extension.py +++ b/flask_apispec/extension.py @@ -35,14 +35,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) @@ -53,8 +56,10 @@ def init_app(self, app): make_apispec(self.app.config.get('APISPEC_TITLE', 'flask-apispec'), self.app.config.get('APISPEC_VERSION', 'v1')) 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() From 7a27abf4b1b8c6b342caee058d6604170c0815f8 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Mon, 25 May 2020 13:29:39 -0400 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.rst | 8 ++++++++ 1 file changed, 8 insertions(+) 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) ******************