-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow schema = None
. Deprecate exclude_from_schema
#5422
Changes from 6 commits
8e3844c
6b8bbe8
f346118
9d84e0c
1c2c4f4
a966b84
d4a4905
6c377a3
f349d35
0cd24b4
853dd23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,12 +8,15 @@ | |||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
from rest_framework import filters, pagination, permissions, serializers | ||||||||||||||||||||||||||||||||||||||||
from rest_framework.compat import coreapi, coreschema | ||||||||||||||||||||||||||||||||||||||||
from rest_framework.decorators import detail_route, list_route | ||||||||||||||||||||||||||||||||||||||||
from rest_framework.decorators import ( | ||||||||||||||||||||||||||||||||||||||||
api_view, detail_route, list_route, schema | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
from rest_framework.request import Request | ||||||||||||||||||||||||||||||||||||||||
from rest_framework.routers import DefaultRouter | ||||||||||||||||||||||||||||||||||||||||
from rest_framework.schemas import ( | ||||||||||||||||||||||||||||||||||||||||
AutoSchema, ManualSchema, SchemaGenerator, get_schema_view | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
from rest_framework.schemas.generators import EndpointEnumerator | ||||||||||||||||||||||||||||||||||||||||
from rest_framework.test import APIClient, APIRequestFactory | ||||||||||||||||||||||||||||||||||||||||
from rest_framework.utils import formatting | ||||||||||||||||||||||||||||||||||||||||
from rest_framework.views import APIView | ||||||||||||||||||||||||||||||||||||||||
|
@@ -613,3 +616,95 @@ def post(self, request, *args, **kwargs): | |||||||||||||||||||||||||||||||||||||||
descr = schema.get_description('example', 'get') | ||||||||||||||||||||||||||||||||||||||||
# the first and last character are '\n' correctly removed by get_description | ||||||||||||||||||||||||||||||||||||||||
assert descr == formatting.dedent(ExampleDocstringAPIView.__doc__[1:][:-1]) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# Views for SchemaGenerationExclusionTests | ||||||||||||||||||||||||||||||||||||||||
class ExcludedAPIView(APIView): | ||||||||||||||||||||||||||||||||||||||||
schema = None | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def get(self, request, *args, **kwargs): | ||||||||||||||||||||||||||||||||||||||||
pass | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
@api_view(['GET']) | ||||||||||||||||||||||||||||||||||||||||
@schema(None) | ||||||||||||||||||||||||||||||||||||||||
def excluded_fbv(request): | ||||||||||||||||||||||||||||||||||||||||
pass | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
@api_view(['GET']) | ||||||||||||||||||||||||||||||||||||||||
def included_fbv(request): | ||||||||||||||||||||||||||||||||||||||||
pass | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
@unittest.skipUnless(coreapi, 'coreapi is not installed') | ||||||||||||||||||||||||||||||||||||||||
class SchemaGenerationExclusionTests(TestCase): | ||||||||||||||||||||||||||||||||||||||||
def setUp(self): | ||||||||||||||||||||||||||||||||||||||||
self.patterns = [ | ||||||||||||||||||||||||||||||||||||||||
url('^excluded-cbv/$', ExcludedAPIView.as_view()), | ||||||||||||||||||||||||||||||||||||||||
url('^excluded-fbv/$', excluded_fbv), | ||||||||||||||||||||||||||||||||||||||||
url('^included-fbv/$', included_fbv), | ||||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_schema_generator_excludes_correctly(self): | ||||||||||||||||||||||||||||||||||||||||
"""Schema should not include excluded views""" | ||||||||||||||||||||||||||||||||||||||||
generator = SchemaGenerator(title='Exclusions', patterns=self.patterns) | ||||||||||||||||||||||||||||||||||||||||
schema = generator.get_schema() | ||||||||||||||||||||||||||||||||||||||||
expected = coreapi.Document( | ||||||||||||||||||||||||||||||||||||||||
url='', | ||||||||||||||||||||||||||||||||||||||||
title='Exclusions', | ||||||||||||||||||||||||||||||||||||||||
content={ | ||||||||||||||||||||||||||||||||||||||||
'included-fbv': { | ||||||||||||||||||||||||||||||||||||||||
'list': coreapi.Link(url='/included-fbv/', action='get') | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
assert len(schema.data) == 1 | ||||||||||||||||||||||||||||||||||||||||
assert 'included-fbv' in schema.data | ||||||||||||||||||||||||||||||||||||||||
assert schema == expected | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_endpoint_enumerator_excludes_correctly(self): | ||||||||||||||||||||||||||||||||||||||||
"""It is responsibility of EndpointEnumerator to exclude views""" | ||||||||||||||||||||||||||||||||||||||||
inspector = EndpointEnumerator(self.patterns) | ||||||||||||||||||||||||||||||||||||||||
endpoints = inspector.get_api_endpoints() | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
assert len(endpoints) == 1 | ||||||||||||||||||||||||||||||||||||||||
path, method, callback = endpoints[0] | ||||||||||||||||||||||||||||||||||||||||
assert path == '/included-fbv/' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_should_include_endpoint_excludes_correctly(self): | ||||||||||||||||||||||||||||||||||||||||
"""This is the specific method that should handle the exclusion""" | ||||||||||||||||||||||||||||||||||||||||
inspector = EndpointEnumerator(self.patterns) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# Not pretty. Mimics internals of EndpointEnumerator to put should_include_endpoint under test | ||||||||||||||||||||||||||||||||||||||||
pairs = [(inspector.get_path_from_regex(pattern.regex.pattern), pattern.callback) | ||||||||||||||||||||||||||||||||||||||||
for pattern in self.patterns] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
should_include = [ | ||||||||||||||||||||||||||||||||||||||||
inspector.should_include_endpoint(*pair) for pair in pairs | ||||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
expected = [False, False, True] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
assert should_include == expected | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_deprecations(self): | ||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rpkilby I'm getting console noise in the test run from this. (Well from the call site but...)
Can you advise on the best way to keep that quiet? Ta! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure we need to test this. Don't believe that we've done so when deprecating other bits of API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. django-rest-framework/rest_framework/filters.py Lines 48 to 52 in efff9ff
and django-rest-framework/tests/test_filters.py Lines 172 to 185 in efff9ff
It's something @rpkilby always puts in. I thought, Why not? 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
https://docs.pytest.org/en/latest/warnings.html#ensuring-function-triggers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rpkilby OK. Thanks. I read that too but was still getting the noise...
(There is plenty of noise from py.test in travis but I assume that'll disappear with time.) Lets call this done. Thanks for the input! @tomchristie I think we're good to go. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No noise when running the tests on my machine, so that would make sense. |
||||||||||||||||||||||||||||||||||||||||
with pytest.warns(PendingDeprecationWarning): | ||||||||||||||||||||||||||||||||||||||||
@api_view(["GET"], exclude_from_schema=True) | ||||||||||||||||||||||||||||||||||||||||
def view(request): | ||||||||||||||||||||||||||||||||||||||||
pass | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
class OldFashjonedExcludedView(APIView): | ||||||||||||||||||||||||||||||||||||||||
exclude_from_schema = True | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def get(self, request, *args, **kwargs): | ||||||||||||||||||||||||||||||||||||||||
pass | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
patterns = [ | ||||||||||||||||||||||||||||||||||||||||
url('^excluded-old-fashioned/$', OldFashjonedExcludedView.as_view()), | ||||||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
inspector = EndpointEnumerator(patterns) | ||||||||||||||||||||||||||||||||||||||||
with pytest.warns(PendingDeprecationWarning): | ||||||||||||||||||||||||||||||||||||||||
inspector.get_api_endpoints() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"is pending deprecation"