mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-07 13:54:47 +03:00
Workaround for multiple http methods for action
This commit is contained in:
parent
ad3eec9afa
commit
963cf95753
|
@ -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)
|
||||
|
|
|
@ -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',
|
||||
|
|
Loading…
Reference in New Issue
Block a user