From d3f8ae4a5d92dcd06c02ec5e306d84182853e312 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Wed, 9 Apr 2025 12:37:50 +0200 Subject: [PATCH 1/3] abstract away allowed actions and methods, and use a set for faster membership checks --- rest_framework/schemas/coreapi.py | 6 +++--- rest_framework/schemas/openapi.py | 6 +++--- rest_framework/schemas/utils.py | 3 +++ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/rest_framework/schemas/coreapi.py b/rest_framework/schemas/coreapi.py index 657178304a..41f3155c54 100644 --- a/rest_framework/schemas/coreapi.py +++ b/rest_framework/schemas/coreapi.py @@ -11,7 +11,7 @@ from .generators import BaseSchemaGenerator from .inspectors import ViewInspector -from .utils import get_pk_description, is_list_view +from .utils import get_pk_description, is_list_view, ALLOW_FILTER_ACTIONS, ALLOW_FILTER_METHODS def common_path(paths): @@ -522,9 +522,9 @@ def _allows_filters(self, path, method): return False if hasattr(self.view, 'action'): - return self.view.action in ["list", "retrieve", "update", "partial_update", "destroy"] + return self.view.action in ALLOW_FILTER_ACTIONS - return method.lower() in ["get", "put", "patch", "delete"] + return method.lower() in ALLOW_FILTER_METHODS def get_filter_fields(self, path, method): if not self._allows_filters(path, method): diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index eb7dc909d9..c7f5d3a7be 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -18,7 +18,7 @@ from .generators import BaseSchemaGenerator from .inspectors import ViewInspector -from .utils import get_pk_description, is_list_view +from .utils import get_pk_description, is_list_view, ALLOW_FILTER_ACTIONS, ALLOW_FILTER_METHODS class SchemaGenerator(BaseSchemaGenerator): @@ -320,8 +320,8 @@ def allows_filters(self, path, method): if getattr(self.view, 'filter_backends', None) is None: return False if hasattr(self.view, 'action'): - return self.view.action in ["list", "retrieve", "update", "partial_update", "destroy"] - return method.lower() in ["get", "put", "patch", "delete"] + return self.view.action in ALLOW_FILTER_ACTIONS + return method.lower() in ALLOW_FILTER_METHODS def get_pagination_parameters(self, path, method): view = self.view diff --git a/rest_framework/schemas/utils.py b/rest_framework/schemas/utils.py index 60ed698294..ffa894e9fc 100644 --- a/rest_framework/schemas/utils.py +++ b/rest_framework/schemas/utils.py @@ -39,3 +39,6 @@ def get_pk_description(model, model_field): value_type=value_type, name=model._meta.verbose_name, ) + +ALLOW_FILTER_ACTIONS = {"list", "retrieve", "update", "partial_update", "destroy"} +ALLOW_FILTER_METHODS = {"get", "put", "patch", "delete"} From 573240e74512e203f29f314af14148b2a488b4e4 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Wed, 9 Apr 2025 12:51:18 +0200 Subject: [PATCH 2/3] rewrite import --- rest_framework/schemas/coreapi.py | 5 ++++- rest_framework/schemas/openapi.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rest_framework/schemas/coreapi.py b/rest_framework/schemas/coreapi.py index 41f3155c54..4a1ecc0f29 100644 --- a/rest_framework/schemas/coreapi.py +++ b/rest_framework/schemas/coreapi.py @@ -11,7 +11,10 @@ from .generators import BaseSchemaGenerator from .inspectors import ViewInspector -from .utils import get_pk_description, is_list_view, ALLOW_FILTER_ACTIONS, ALLOW_FILTER_METHODS +from .utils import ( + ALLOW_FILTER_ACTIONS, ALLOW_FILTER_METHODS, get_pk_description, + is_list_view +) def common_path(paths): diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index c7f5d3a7be..b84702fe88 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -18,7 +18,10 @@ from .generators import BaseSchemaGenerator from .inspectors import ViewInspector -from .utils import get_pk_description, is_list_view, ALLOW_FILTER_ACTIONS, ALLOW_FILTER_METHODS +from .utils import ( + ALLOW_FILTER_ACTIONS, ALLOW_FILTER_METHODS, get_pk_description, + is_list_view +) class SchemaGenerator(BaseSchemaGenerator): From 0316a9a8b01429058da9707509207d9672205be3 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Wed, 9 Apr 2025 12:57:01 +0200 Subject: [PATCH 3/3] rewrite import --- rest_framework/schemas/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/rest_framework/schemas/utils.py b/rest_framework/schemas/utils.py index ffa894e9fc..84690670e7 100644 --- a/rest_framework/schemas/utils.py +++ b/rest_framework/schemas/utils.py @@ -40,5 +40,6 @@ def get_pk_description(model, model_field): name=model._meta.verbose_name, ) + ALLOW_FILTER_ACTIONS = {"list", "retrieve", "update", "partial_update", "destroy"} ALLOW_FILTER_METHODS = {"get", "put", "patch", "delete"}