Skip to content

Commit fde309d

Browse files
committed
Fixed encode#7007: Added Deprecation warning for coreapi
1 parent 376a5cb commit fde309d

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

rest_framework/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@
3131

3232
class RemovedInDRF315Warning(DeprecationWarning):
3333
pass
34+
35+
36+
class RemovedInDRF317Warning(DeprecationWarning):
37+
pass

rest_framework/filters.py

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
returned by list views.
44
"""
55
import operator
6+
import warnings
67
from functools import reduce
78

89
from django.core.exceptions import ImproperlyConfigured
@@ -12,6 +13,7 @@
1213
from django.utils.encoding import force_str
1314
from django.utils.translation import gettext_lazy as _
1415

16+
from rest_framework import RemovedInDRF317Warning
1517
from rest_framework.compat import coreapi, coreschema, distinct
1618
from rest_framework.settings import api_settings
1719

@@ -29,6 +31,8 @@ def filter_queryset(self, request, queryset, view):
2931

3032
def get_schema_fields(self, view):
3133
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
34+
if coreapi is not None:
35+
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
3236
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
3337
return []
3438

@@ -146,6 +150,8 @@ def to_html(self, request, queryset, view):
146150

147151
def get_schema_fields(self, view):
148152
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
153+
if coreapi is not None:
154+
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
149155
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
150156
return [
151157
coreapi.Field(
@@ -306,6 +312,8 @@ def to_html(self, request, queryset, view):
306312

307313
def get_schema_fields(self, view):
308314
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
315+
if coreapi is not None:
316+
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
309317
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
310318
return [
311319
coreapi.Field(

rest_framework/pagination.py

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""
55

66
import contextlib
7+
import warnings
78
from base64 import b64decode, b64encode
89
from collections import namedtuple
910
from urllib import parse
@@ -15,6 +16,7 @@
1516
from django.utils.encoding import force_str
1617
from django.utils.translation import gettext_lazy as _
1718

19+
from rest_framework import RemovedInDRF317Warning
1820
from rest_framework.compat import coreapi, coreschema
1921
from rest_framework.exceptions import NotFound
2022
from rest_framework.response import Response
@@ -152,6 +154,8 @@ def get_results(self, data):
152154

153155
def get_schema_fields(self, view):
154156
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
157+
if coreapi is not None:
158+
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
155159
return []
156160

157161
def get_schema_operation_parameters(self, view):
@@ -311,6 +315,8 @@ def to_html(self):
311315

312316
def get_schema_fields(self, view):
313317
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
318+
if coreapi is not None:
319+
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
314320
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
315321
fields = [
316322
coreapi.Field(
@@ -525,6 +531,8 @@ def get_count(self, queryset):
525531

526532
def get_schema_fields(self, view):
527533
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
534+
if coreapi is not None:
535+
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
528536
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
529537
return [
530538
coreapi.Field(
@@ -930,6 +938,8 @@ def to_html(self):
930938

931939
def get_schema_fields(self, view):
932940
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
941+
if coreapi is not None:
942+
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
933943
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
934944
fields = [
935945
coreapi.Field(

rest_framework/schemas/coreapi.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.db import models
66
from django.utils.encoding import force_str
77

8-
from rest_framework import exceptions, serializers
8+
from rest_framework import RemovedInDRF317Warning, exceptions, serializers
99
from rest_framework.compat import coreapi, coreschema, uritemplate
1010
from rest_framework.settings import api_settings
1111

@@ -118,6 +118,8 @@ class SchemaGenerator(BaseSchemaGenerator):
118118

119119
def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None, version=None):
120120
assert coreapi, '`coreapi` must be installed for schema support.'
121+
if coreapi is not None:
122+
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
121123
assert coreschema, '`coreschema` must be installed for schema support.'
122124

123125
super().__init__(title, url, description, patterns, urlconf)

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ license_files = LICENSE.md
33

44
[tool:pytest]
55
addopts=--tb=short --strict-markers -ra
6+
testspath = tests
7+
filterwarnings = ignore:CoreAPI compatibility is deprecated*:rest_framework.RemovedInDRF317Warning
68

79
[flake8]
810
ignore = E501,W503,W504

tests/schemas/test_coreapi.py

+44-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@
77
from django.urls import include, path
88

99
from rest_framework import (
10-
filters, generics, pagination, permissions, serializers
10+
RemovedInDRF317Warning, filters, generics, pagination, permissions,
11+
serializers
1112
)
1213
from rest_framework.compat import coreapi, coreschema
1314
from rest_framework.decorators import action, api_view, schema
15+
from rest_framework.filters import (
16+
BaseFilterBackend, OrderingFilter, SearchFilter
17+
)
18+
from rest_framework.pagination import (
19+
BasePagination, CursorPagination, LimitOffsetPagination,
20+
PageNumberPagination
21+
)
1422
from rest_framework.request import Request
1523
from rest_framework.routers import DefaultRouter, SimpleRouter
1624
from rest_framework.schemas import (
@@ -1433,3 +1441,38 @@ def test_schema_handles_exception():
14331441
response.render()
14341442
assert response.status_code == 403
14351443
assert b"You do not have permission to perform this action." in response.content
1444+
1445+
1446+
class CoreapiDeprecationTestCase(TestCase):
1447+
def assert_deprecation_warning(self, obj):
1448+
with pytest.warns(RemovedInDRF317Warning) as warning_list:
1449+
obj.get_schema_fields({})
1450+
assert len(warning_list) == 1
1451+
assert str(warning_list[0].message) == "CoreAPI compatibility is deprecated and will be removed in DRF 3.17"
1452+
1453+
def test_filter_backend_deprecation_warning(self):
1454+
filter_backends = [
1455+
SearchFilter(),
1456+
BaseFilterBackend(),
1457+
OrderingFilter(),
1458+
]
1459+
1460+
for obj in filter_backends:
1461+
self.assert_deprecation_warning(obj)
1462+
1463+
def test_pagination_deprecation_warning(self):
1464+
pagination_classes = [
1465+
BasePagination(),
1466+
PageNumberPagination(),
1467+
LimitOffsetPagination(),
1468+
CursorPagination(),
1469+
]
1470+
1471+
for obj in pagination_classes:
1472+
self.assert_deprecation_warning(obj)
1473+
1474+
def test_schema_generator_deprecation_warning(self):
1475+
with pytest.warns(RemovedInDRF317Warning) as warning_list:
1476+
SchemaGenerator()
1477+
assert len(warning_list) == 1
1478+
assert str(warning_list[0].message) == "CoreAPI compatibility is deprecated and will be removed in DRF 3.17"

0 commit comments

Comments
 (0)