Skip to content

Commit

Permalink
Deprecated built-in schema generation.
Browse files Browse the repository at this point in the history
* To be removed in v25.
* Use drf-spectacular.

Closes #1432. Closes #1573.
  • Loading branch information
carltongibson committed Apr 23, 2023
1 parent 90768fe commit 01a83fb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
14 changes: 14 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -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 <https://drf-spectacular.readthedocs.io/en/latest/>`_
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)
------------------------

Expand Down
8 changes: 8 additions & 0 deletions django_filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,11 @@ def parse_version(version):


VERSION = parse_version(__version__)



assert VERSION < (25,0), "Remove deprecated code"


class RemovedInDjangoFilter25Warning(DeprecationWarning):
pass
10 changes: 10 additions & 0 deletions django_filters/rest_framework/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<name>_0`, `<name>_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()`"
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion requirements/test-ci.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
markdown
coreapi
django-crispy-forms

coverage
Expand Down
13 changes: 11 additions & 2 deletions tests/rest_framework/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 RemovedInDjangoFilter25Warning, compat, filters
from django_filters.rest_framework import DjangoFilterBackend, FilterSet, backends

from ..models import Article
Expand Down Expand Up @@ -245,13 +245,15 @@ 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())
fields = [f["name"] for f in fields]

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())
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 01a83fb

Please sign in to comment.