Skip to content

Commit 9731269

Browse files
authored
Merge pull request #5085 from hurturk/schema-method-limited
Generate schema with respect to http_method_names provided by CBV
2 parents a1e6d83 + 518bb44 commit 9731269

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

rest_framework/schemas.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ def get_allowed_methods(self, callback):
246246
Return a list of the valid HTTP methods for this endpoint.
247247
"""
248248
if hasattr(callback, 'actions'):
249-
return [method.upper() for method in callback.actions.keys()]
249+
actions = set(callback.actions.keys())
250+
http_method_names = set(callback.cls.http_method_names)
251+
return [method.upper() for method in actions & http_method_names]
250252

251253
return [
252254
method for method in

tests/test_schemas.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ class PermissionDeniedExampleViewSet(ExampleViewSet):
246246
permission_classes = [DenyAllUsingPermissionDenied]
247247

248248

249+
class MethodLimitedViewSet(ExampleViewSet):
250+
permission_classes = []
251+
http_method_names = ['get', 'head', 'options']
252+
253+
249254
class ExampleListView(APIView):
250255
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
251256

@@ -368,6 +373,61 @@ def test_schema_for_regular_views(self):
368373
assert schema == expected
369374

370375

376+
@unittest.skipUnless(coreapi, 'coreapi is not installed')
377+
class TestSchemaGeneratorWithMethodLimitedViewSets(TestCase):
378+
def setUp(self):
379+
router = DefaultRouter()
380+
router.register('example1', MethodLimitedViewSet, base_name='example1')
381+
self.patterns = [
382+
url(r'^', include(router.urls))
383+
]
384+
385+
def test_schema_for_regular_views(self):
386+
"""
387+
Ensure that schema generation works for ViewSet classes
388+
with method limitation by Django CBV's http_method_names attribute
389+
"""
390+
generator = SchemaGenerator(title='Example API', patterns=self.patterns)
391+
request = factory.get('/example1/')
392+
schema = generator.get_schema(Request(request))
393+
394+
expected = coreapi.Document(
395+
url='http://testserver/example1/',
396+
title='Example API',
397+
content={
398+
'example1': {
399+
'list': coreapi.Link(
400+
url='/example1/',
401+
action='get',
402+
fields=[
403+
coreapi.Field('page', required=False, location='query', schema=coreschema.Integer(title='Page', description='A page number within the paginated result set.')),
404+
coreapi.Field('page_size', required=False, location='query', schema=coreschema.Integer(title='Page size', description='Number of results to return per page.')),
405+
coreapi.Field('ordering', required=False, location='query', schema=coreschema.String(title='Ordering', description='Which field to use when ordering the results.'))
406+
]
407+
),
408+
'custom_list_action': coreapi.Link(
409+
url='/example1/custom_list_action/',
410+
action='get'
411+
),
412+
'custom_list_action_multiple_methods': {
413+
'read': coreapi.Link(
414+
url='/example1/custom_list_action_multiple_methods/',
415+
action='get'
416+
)
417+
},
418+
'read': coreapi.Link(
419+
url='/example1/{id}/',
420+
action='get',
421+
fields=[
422+
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
423+
]
424+
)
425+
}
426+
}
427+
)
428+
assert schema == expected
429+
430+
371431
@unittest.skipUnless(coreapi, 'coreapi is not installed')
372432
class TestSchemaGeneratorWithRestrictedViewSets(TestCase):
373433
def setUp(self):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy