From ed7b2d7410b0684fe8135b4b07e3739cb4ae93c0 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Sun, 23 Apr 2023 18:00:57 +0200 Subject: [PATCH] Deprecated built-in schema generation. * To be removed in v25. * Use drf-spectacular. Closes #1432. Closes #1573. --- CHANGES.rst | 14 ++++++++++++++ django_filters/__init__.py | 8 ++++++++ django_filters/rest_framework/backends.py | 10 ++++++++++ requirements/test-ci.txt | 1 - tests/rest_framework/test_backends.py | 13 +++++++++++-- 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 7425769c..74dc2f8d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,17 @@ +Version 23.2 (UNRELEASED) +------------------------- + +* Deprecated the schema generation methods of the DRF related ``DjangoFilterBackend``. + These will be removed in version 25.1. + + You should use `drf-spectacular `_ + for generating OpenAPI schemas with DRF. + +* In addition, stopped testing against the (very old now) ``coreapi`` schema generation. + These methods should continue to work if you're using them until v25.1, but + ``coreapi`` is no longer maintained, and is raising warnings against the current + versions of Python. To workaround this is not worth the effort at this point. + Version 23.1 (2023-3-26) ------------------------ diff --git a/django_filters/__init__.py b/django_filters/__init__.py index 0c26effa..7ea8996b 100644 --- a/django_filters/__init__.py +++ b/django_filters/__init__.py @@ -29,3 +29,11 @@ def parse_version(version): VERSION = parse_version(__version__) + + + +assert VERSION < (25,0), "Remove deprecated code" + + +class RemovedInDjangoFilter25Warning(DeprecationWarning): + pass diff --git a/django_filters/rest_framework/backends.py b/django_filters/rest_framework/backends.py index b0081f9f..bb2ae352 100644 --- a/django_filters/rest_framework/backends.py +++ b/django_filters/rest_framework/backends.py @@ -93,6 +93,11 @@ def get_schema_fields(self, view): # This is not compatible with widgets where the query param differs from the # filter's attribute name. Notably, this includes `MultiWidget`, where query # params will be of the format `_0`, `_1`, etc... + from django_filters import RemovedInDjangoFilter25Warning + warnings.warn( + "Built-in schema generation is deprecated. Use drf-spectacular.", + category=RemovedInDjangoFilter25Warning, + ) assert ( compat.coreapi is not None ), "coreapi must be installed to use `get_schema_fields()`" @@ -125,6 +130,11 @@ def get_schema_fields(self, view): ) def get_schema_operation_parameters(self, view): + from django_filters import RemovedInDjangoFilter25Warning + warnings.warn( + "Built-in schema generation is deprecated. Use drf-spectacular.", + category=RemovedInDjangoFilter25Warning, + ) try: queryset = view.get_queryset() except Exception: diff --git a/requirements/test-ci.txt b/requirements/test-ci.txt index 79f97f17..2f787bf9 100644 --- a/requirements/test-ci.txt +++ b/requirements/test-ci.txt @@ -1,5 +1,4 @@ markdown -coreapi django-crispy-forms coverage diff --git a/tests/rest_framework/test_backends.py b/tests/rest_framework/test_backends.py index d4b8a031..4038b59e 100644 --- a/tests/rest_framework/test_backends.py +++ b/tests/rest_framework/test_backends.py @@ -3,11 +3,11 @@ from django.db.models import BooleanField from django.test import TestCase -from django.test.utils import override_settings +from django.test.utils import ignore_warnings, override_settings from rest_framework import generics, serializers from rest_framework.test import APIRequestFactory -from django_filters import compat, filters +from django_filters import compat, filters, RemovedInDjangoFilter25Warning from django_filters.rest_framework import DjangoFilterBackend, FilterSet, backends from ..models import Article @@ -245,6 +245,7 @@ class View(FilterClassRootView): class GetSchemaOperationParametersTests(TestCase): + @ignore_warnings(category=RemovedInDjangoFilter25Warning) def test_get_operation_parameters_with_filterset_fields_list(self): backend = DjangoFilterBackend() fields = backend.get_schema_operation_parameters(FilterFieldsRootView()) @@ -252,6 +253,7 @@ def test_get_operation_parameters_with_filterset_fields_list(self): self.assertEqual(fields, ["decimal", "date"]) + @ignore_warnings(category=RemovedInDjangoFilter25Warning) def test_get_operation_parameters_with_filterset_fields_list_with_choices(self): backend = DjangoFilterBackend() fields = backend.get_schema_operation_parameters(CategoryItemView()) @@ -269,6 +271,12 @@ def test_get_operation_parameters_with_filterset_fields_list_with_choices(self): ], ) + def test_deprecation_warning(self): + backend = DjangoFilterBackend() + msg = "Built-in schema generation is deprecated. Use drf-spectacular." + with self.assertRaisesMessage(RemovedInDjangoFilter25Warning, msg): + backend.get_schema_operation_parameters(FilterFieldsRootView()) + class TemplateTests(TestCase): def test_backend_output(self): @@ -407,6 +415,7 @@ def test_to_html_none_filter_class(self): html = self.backend.to_html(mock.Mock(), mock.Mock(), mock.Mock()) self.assertIsNone(html) + @ignore_warnings(category=RemovedInDjangoFilter25Warning) def test_get_schema_operation_parameters_userwarning(self): with self.assertWarns(UserWarning): view = mock.Mock()