Workaround for multiple http methods for action

This commit is contained in:
Nik 2016-08-24 18:47:50 +03:00
parent ad3eec9afa
commit 963cf95753
2 changed files with 26 additions and 6 deletions

View File

@ -101,16 +101,26 @@ class SchemaGenerator(object):
if not links: if not links:
return None 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: # Generate the schema content structure, eg:
# {'users': {'list': Link()}} # {'users': {'list': Link()}}
content = {} content = {}
for category, action, link in links: 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: if category is None:
content[action] = link content[action] = link
elif category in content:
content[category][action] = link
else: else:
content[category] = {action: link} if category not in content:
content[category] = {}
content[category][action] = link
# Return the schema document. # Return the schema document.
return coreapi.Document(title=self.title, content=content, url=self.url) return coreapi.Document(title=self.title, content=content, url=self.url)

View File

@ -145,7 +145,7 @@ class TestRouterGeneratedSchema(TestCase):
coreapi.Field('pk', required=True, location='path') coreapi.Field('pk', required=True, location='path')
] ]
), ),
'custom_action': coreapi.Link( 'custom_action_post': coreapi.Link(
url='/example/{pk}/custom_action/', url='/example/{pk}/custom_action/',
action='post', action='post',
encoding='application/json', encoding='application/json',
@ -155,6 +155,16 @@ class TestRouterGeneratedSchema(TestCase):
coreapi.Field('d', required=False, location='form'), 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( 'custom_list_action': coreapi.Link(
url='/example/custom_list_action/', url='/example/custom_list_action/',
action='get' action='get'
@ -195,7 +205,7 @@ class TestRouterGeneratedSchema(TestCase):
client = APIClient() client = APIClient()
client.force_authenticate(MockUser()) client.force_authenticate(MockUser())
response = client.get('/', HTTP_ACCEPT='application/vnd.coreapi+json') response = client.get('/', HTTP_ACCEPT='application/vnd.coreapi+json')
put_action = ('custom_action', put_action = ('custom_action_put',
coreapi.Link( coreapi.Link(
url='/example/{pk}/custom_action/', url='/example/{pk}/custom_action/',
action='put', action='put',
@ -206,7 +216,7 @@ class TestRouterGeneratedSchema(TestCase):
coreapi.Field('d', required=False, location='form'), coreapi.Field('d', required=False, location='form'),
] ]
)) ))
post_action = ('custom_action', post_action = ('custom_action_post',
coreapi.Link( coreapi.Link(
url='/example/{pk}/custom_action/', url='/example/{pk}/custom_action/',
action='post', action='post',