diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index 1b899450f..b0b11797d 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -101,16 +101,26 @@ class SchemaGenerator(object): if not links: return None + # looks for overlapped actions for multiple http methods per action case + overlapped = {} + for category, action, link in links: + key = (category, action) + overlapped[key] = key in overlapped + # Generate the schema content structure, eg: # {'users': {'list': Link()}} content = {} for category, action, link in links: + # add suffix for overlapped actions + if overlapped[(category, action)]: + action = '%s_%s' % (action, link.action) if category is None: content[action] = link - elif category in content: - content[category][action] = link else: - content[category] = {action: link} + if category not in content: + content[category] = {} + + content[category][action] = link # Return the schema document. return coreapi.Document(title=self.title, content=content, url=self.url) diff --git a/tests/test_schemas.py b/tests/test_schemas.py index ae9ea7ae3..e8a8cdb6b 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -145,7 +145,7 @@ class TestRouterGeneratedSchema(TestCase): coreapi.Field('pk', required=True, location='path') ] ), - 'custom_action': coreapi.Link( + 'custom_action_post': coreapi.Link( url='/example/{pk}/custom_action/', action='post', encoding='application/json', @@ -155,6 +155,16 @@ class TestRouterGeneratedSchema(TestCase): coreapi.Field('d', required=False, location='form'), ] ), + 'custom_action_put': coreapi.Link( + url='/example/{pk}/custom_action/', + action='put', + encoding='application/json', + fields=[ + coreapi.Field('pk', required=True, location='path'), + coreapi.Field('c', required=True, location='form'), + coreapi.Field('d', required=False, location='form'), + ] + ), 'custom_list_action': coreapi.Link( url='/example/custom_list_action/', action='get' @@ -195,7 +205,7 @@ class TestRouterGeneratedSchema(TestCase): client = APIClient() client.force_authenticate(MockUser()) response = client.get('/', HTTP_ACCEPT='application/vnd.coreapi+json') - put_action = ('custom_action', + put_action = ('custom_action_put', coreapi.Link( url='/example/{pk}/custom_action/', action='put', @@ -206,7 +216,7 @@ class TestRouterGeneratedSchema(TestCase): coreapi.Field('d', required=False, location='form'), ] )) - post_action = ('custom_action', + post_action = ('custom_action_post', coreapi.Link( url='/example/{pk}/custom_action/', action='post',