mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-18 12:12:19 +03:00
Added Deprecation Warnings for CoreAPI
This commit is contained in:
parent
eff97efa28
commit
560e581003
|
@ -3,6 +3,7 @@ Provides generic filtering backends that can be used to filter the results
|
|||
returned by list views.
|
||||
"""
|
||||
import operator
|
||||
import warnings
|
||||
from functools import reduce
|
||||
|
||||
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.translation import gettext_lazy as _
|
||||
|
||||
from rest_framework import RemovedInDRF314Warning
|
||||
from rest_framework.compat import coreapi, coreschema, distinct
|
||||
from rest_framework.settings import api_settings
|
||||
|
||||
|
@ -29,6 +31,8 @@ class BaseFilterBackend:
|
|||
|
||||
def get_schema_fields(self, view):
|
||||
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.14', RemovedInDRF314Warning)
|
||||
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
|
||||
return []
|
||||
|
||||
|
@ -146,6 +150,8 @@ class SearchFilter(BaseFilterBackend):
|
|||
|
||||
def get_schema_fields(self, view):
|
||||
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.14', RemovedInDRF314Warning)
|
||||
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
|
||||
return [
|
||||
coreapi.Field(
|
||||
|
@ -296,6 +302,8 @@ class OrderingFilter(BaseFilterBackend):
|
|||
|
||||
def get_schema_fields(self, view):
|
||||
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.14', RemovedInDRF314Warning)
|
||||
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
|
||||
return [
|
||||
coreapi.Field(
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
Pagination serializers determine the structure of the output that should
|
||||
be used for paginated responses.
|
||||
"""
|
||||
import warnings
|
||||
from base64 import b64decode, b64encode
|
||||
from collections import OrderedDict, namedtuple
|
||||
from urllib import parse
|
||||
|
@ -12,6 +13,7 @@ from django.template import loader
|
|||
from django.utils.encoding import force_str
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from rest_framework import RemovedInDRF314Warning
|
||||
from rest_framework.compat import coreapi, coreschema
|
||||
from rest_framework.exceptions import NotFound
|
||||
from rest_framework.response import Response
|
||||
|
@ -149,6 +151,8 @@ class BasePagination:
|
|||
|
||||
def get_schema_fields(self, view):
|
||||
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.14', RemovedInDRF314Warning)
|
||||
return []
|
||||
|
||||
def get_schema_operation_parameters(self, view):
|
||||
|
@ -307,6 +311,8 @@ class PageNumberPagination(BasePagination):
|
|||
|
||||
def get_schema_fields(self, view):
|
||||
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.14', RemovedInDRF314Warning)
|
||||
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
|
||||
fields = [
|
||||
coreapi.Field(
|
||||
|
@ -525,6 +531,8 @@ class LimitOffsetPagination(BasePagination):
|
|||
|
||||
def get_schema_fields(self, view):
|
||||
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.14', RemovedInDRF314Warning)
|
||||
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
|
||||
return [
|
||||
coreapi.Field(
|
||||
|
@ -924,6 +932,8 @@ class CursorPagination(BasePagination):
|
|||
|
||||
def get_schema_fields(self, view):
|
||||
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.14', RemovedInDRF314Warning)
|
||||
assert coreschema is not None, 'coreschema must be installed to use `get_schema_fields()`'
|
||||
fields = [
|
||||
coreapi.Field(
|
||||
|
|
|
@ -5,7 +5,7 @@ from urllib import parse
|
|||
from django.db import models
|
||||
from django.utils.encoding import force_str
|
||||
|
||||
from rest_framework import exceptions, serializers
|
||||
from rest_framework import RemovedInDRF314Warning, exceptions, serializers
|
||||
from rest_framework.compat import coreapi, coreschema, uritemplate
|
||||
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):
|
||||
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.14', RemovedInDRF314Warning)
|
||||
assert coreschema, '`coreschema` must be installed for schema support.'
|
||||
|
||||
super(SchemaGenerator, self).__init__(title, url, description, patterns, urlconf)
|
||||
|
@ -347,6 +349,8 @@ class AutoSchema(ViewInspector):
|
|||
will be added to auto-generated fields, overwriting on `Field.name`
|
||||
"""
|
||||
super(AutoSchema, self).__init__()
|
||||
if coreapi is not None:
|
||||
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.14', RemovedInDRF314Warning)
|
||||
if manual_fields is None:
|
||||
manual_fields = []
|
||||
self._manual_fields = manual_fields
|
||||
|
@ -588,6 +592,8 @@ class ManualSchema(ViewInspector):
|
|||
* `description`: String description for view. Optional.
|
||||
"""
|
||||
super(ManualSchema, self).__init__()
|
||||
if coreapi is not None:
|
||||
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.14', RemovedInDRF314Warning)
|
||||
assert all(isinstance(f, coreapi.Field) for f in fields), "`fields` must be a list of coreapi.Field instances"
|
||||
self._fields = fields
|
||||
self._description = description
|
||||
|
@ -609,4 +615,6 @@ class ManualSchema(ViewInspector):
|
|||
|
||||
def is_enabled():
|
||||
"""Is CoreAPI Mode enabled?"""
|
||||
if coreapi is not None:
|
||||
warnings.warn('CoreAPI compatibility is deprecated and will be removed in DRF 3.14', RemovedInDRF314Warning)
|
||||
return issubclass(api_settings.DEFAULT_SCHEMA_CLASS, AutoSchema)
|
||||
|
|
|
@ -4,6 +4,7 @@ license_file = LICENSE.md
|
|||
[tool:pytest]
|
||||
addopts=--tb=short --strict -ra
|
||||
testspath = tests
|
||||
filterwarnings = ignore:CoreAPI compatibility is deprecated*:rest_framework.RemovedInDRF314Warning
|
||||
|
||||
[flake8]
|
||||
ignore = E501,W504
|
||||
|
|
|
@ -7,16 +7,24 @@ from django.test import TestCase, override_settings
|
|||
from django.urls import include, path
|
||||
|
||||
from rest_framework import (
|
||||
filters, generics, pagination, permissions, serializers
|
||||
RemovedInDRF314Warning, filters, generics, pagination, permissions,
|
||||
serializers
|
||||
)
|
||||
from rest_framework.compat import coreapi, coreschema
|
||||
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.routers import DefaultRouter, SimpleRouter
|
||||
from rest_framework.schemas import (
|
||||
AutoSchema, ManualSchema, SchemaGenerator, get_schema_view
|
||||
)
|
||||
from rest_framework.schemas.coreapi import field_to_schema
|
||||
from rest_framework.schemas.coreapi import field_to_schema, is_enabled
|
||||
from rest_framework.schemas.generators import EndpointEnumerator
|
||||
from rest_framework.schemas.utils import is_list_view
|
||||
from rest_framework.test import APIClient, APIRequestFactory
|
||||
|
@ -1372,3 +1380,46 @@ def test_schema_handles_exception():
|
|||
response.render()
|
||||
assert response.status_code == 403
|
||||
assert b"You do not have permission to perform this action." in response.content
|
||||
|
||||
|
||||
@pytest.mark.skipif(not coreapi, reason='coreapi is not installed')
|
||||
def test_coreapi_deprecation():
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
SchemaGenerator()
|
||||
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
AutoSchema()
|
||||
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
ManualSchema({})
|
||||
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
deprecated_filter = OrderingFilter()
|
||||
deprecated_filter.get_schema_fields({})
|
||||
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
deprecated_filter = BaseFilterBackend()
|
||||
deprecated_filter.get_schema_fields({})
|
||||
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
deprecated_filter = SearchFilter()
|
||||
deprecated_filter.get_schema_fields({})
|
||||
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
paginator = BasePagination()
|
||||
paginator.get_schema_fields({})
|
||||
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
paginator = PageNumberPagination()
|
||||
paginator.get_schema_fields({})
|
||||
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
paginator = LimitOffsetPagination()
|
||||
paginator.get_schema_fields({})
|
||||
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
paginator = CursorPagination()
|
||||
paginator.get_schema_fields({})
|
||||
|
||||
with pytest.warns(RemovedInDRF314Warning):
|
||||
is_enabled()
|
||||
|
|
Loading…
Reference in New Issue
Block a user