Fixed #7007: Added Deprecation warning for coreapi

This commit is contained in:
Neeraj Kumar 2023-06-12 02:04:36 +05:30
parent 376a5cbbba
commit fde309d0c4
6 changed files with 71 additions and 2 deletions

View File

@ -31,3 +31,7 @@ if django.VERSION < (3, 2):
class RemovedInDRF315Warning(DeprecationWarning): class RemovedInDRF315Warning(DeprecationWarning):
pass pass
class RemovedInDRF317Warning(DeprecationWarning):
pass

View File

@ -3,6 +3,7 @@ Provides generic filtering backends that can be used to filter the results
returned by list views. returned by list views.
""" """
import operator import operator
import warnings
from functools import reduce from functools import reduce
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -12,6 +13,7 @@ from django.template import loader
from django.utils.encoding import force_str from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from rest_framework import RemovedInDRF317Warning
from rest_framework.compat import coreapi, coreschema, distinct from rest_framework.compat import coreapi, coreschema, distinct
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
@ -29,6 +31,8 @@ class BaseFilterBackend:
def get_schema_fields(self, view): def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`' assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
if coreapi is not None:
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`' assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
return [] return []
@ -146,6 +150,8 @@ class SearchFilter(BaseFilterBackend):
def get_schema_fields(self, view): def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`' assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
if coreapi is not None:
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`' assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
return [ return [
coreapi.Field( coreapi.Field(
@ -306,6 +312,8 @@ class OrderingFilter(BaseFilterBackend):
def get_schema_fields(self, view): def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`' assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
if coreapi is not None:
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`' assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
return [ return [
coreapi.Field( coreapi.Field(

View File

@ -4,6 +4,7 @@ be used for paginated responses.
""" """
import contextlib import contextlib
import warnings
from base64 import b64decode, b64encode from base64 import b64decode, b64encode
from collections import namedtuple from collections import namedtuple
from urllib import parse from urllib import parse
@ -15,6 +16,7 @@ from django.template import loader
from django.utils.encoding import force_str from django.utils.encoding import force_str
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from rest_framework import RemovedInDRF317Warning
from rest_framework.compat import coreapi, coreschema from rest_framework.compat import coreapi, coreschema
from rest_framework.exceptions import NotFound from rest_framework.exceptions import NotFound
from rest_framework.response import Response from rest_framework.response import Response
@ -152,6 +154,8 @@ class BasePagination:
def get_schema_fields(self, view): def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`' assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
if coreapi is not None:
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
return [] return []
def get_schema_operation_parameters(self, view): def get_schema_operation_parameters(self, view):
@ -311,6 +315,8 @@ class PageNumberPagination(BasePagination):
def get_schema_fields(self, view): def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`' assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
if coreapi is not None:
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`' assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
fields = [ fields = [
coreapi.Field( coreapi.Field(
@ -525,6 +531,8 @@ class LimitOffsetPagination(BasePagination):
def get_schema_fields(self, view): def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`' assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
if coreapi is not None:
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`' assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
return [ return [
coreapi.Field( coreapi.Field(
@ -930,6 +938,8 @@ class CursorPagination(BasePagination):
def get_schema_fields(self, view): def get_schema_fields(self, view):
assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`' assert coreapi is not None, 'coreapi must be installed to use `get_schema_fields()`'
if coreapi is not None:
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`' assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
fields = [ fields = [
coreapi.Field( coreapi.Field(

View File

@ -5,7 +5,7 @@ from urllib import parse
from django.db import models from django.db import models
from django.utils.encoding import force_str from django.utils.encoding import force_str
from rest_framework import exceptions, serializers from rest_framework import RemovedInDRF317Warning, exceptions, serializers
from rest_framework.compat import coreapi, coreschema, uritemplate from rest_framework.compat import coreapi, coreschema, uritemplate
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
@ -118,6 +118,8 @@ class SchemaGenerator(BaseSchemaGenerator):
def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None, version=None): def __init__(self, title=None, url=None, description=None, patterns=None, urlconf=None, version=None):
assert coreapi, '`coreapi` must be installed for schema support.' assert coreapi, '`coreapi` must be installed for schema support.'
if coreapi is not None:
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.17', RemovedInDRF317Warning)
assert coreschema, '`coreschema` must be installed for schema support.' assert coreschema, '`coreschema` must be installed for schema support.'
super().__init__(title, url, description, patterns, urlconf) super().__init__(title, url, description, patterns, urlconf)

View File

@ -3,6 +3,8 @@ license_files = LICENSE.md
[tool:pytest] [tool:pytest]
addopts=--tb=short --strict-markers -ra addopts=--tb=short --strict-markers -ra
testspath = tests
filterwarnings = ignore:CoreAPI compatibility is deprecated*:rest_framework.RemovedInDRF317Warning
[flake8] [flake8]
ignore = E501,W503,W504 ignore = E501,W503,W504

View File

@ -7,10 +7,18 @@ from django.test import TestCase, override_settings
from django.urls import include, path from django.urls import include, path
from rest_framework import ( from rest_framework import (
filters, generics, pagination, permissions, serializers RemovedInDRF317Warning, filters, generics, pagination, permissions,
serializers
) )
from rest_framework.compat import coreapi, coreschema from rest_framework.compat import coreapi, coreschema
from rest_framework.decorators import action, api_view, schema from rest_framework.decorators import action, api_view, schema
from rest_framework.filters import (
BaseFilterBackend, OrderingFilter, SearchFilter
)
from rest_framework.pagination import (
BasePagination, CursorPagination, LimitOffsetPagination,
PageNumberPagination
)
from rest_framework.request import Request from rest_framework.request import Request
from rest_framework.routers import DefaultRouter, SimpleRouter from rest_framework.routers import DefaultRouter, SimpleRouter
from rest_framework.schemas import ( from rest_framework.schemas import (
@ -1433,3 +1441,38 @@ def test_schema_handles_exception():
response.render() response.render()
assert response.status_code == 403 assert response.status_code == 403
assert b"You do not have permission to perform this action." in response.content assert b"You do not have permission to perform this action." in response.content
class CoreapiDeprecationTestCase(TestCase):
def assert_deprecation_warning(self, obj):
with pytest.warns(RemovedInDRF317Warning) as warning_list:
obj.get_schema_fields({})
assert len(warning_list) == 1
assert str(warning_list[0].message) == "CoreAPI compatibility is deprecated and will be removed in DRF 3.17"
def test_filter_backend_deprecation_warning(self):
filter_backends = [
SearchFilter(),
BaseFilterBackend(),
OrderingFilter(),
]
for obj in filter_backends:
self.assert_deprecation_warning(obj)
def test_pagination_deprecation_warning(self):
pagination_classes = [
BasePagination(),
PageNumberPagination(),
LimitOffsetPagination(),
CursorPagination(),
]
for obj in pagination_classes:
self.assert_deprecation_warning(obj)
def test_schema_generator_deprecation_warning(self):
with pytest.warns(RemovedInDRF317Warning) as warning_list:
SchemaGenerator()
assert len(warning_list) == 1
assert str(warning_list[0].message) == "CoreAPI compatibility is deprecated and will be removed in DRF 3.17"