This commit is contained in:
Kaleb Elwert 2017-06-06 15:22:29 +00:00 committed by GitHub
commit 773e731916
2 changed files with 124 additions and 76 deletions

View File

@ -649,9 +649,8 @@ class SchemaGenerator(object):
action = self.default_mapping[method.lower()] action = self.default_mapping[method.lower()]
named_path_components = [ named_path_components = [
component for component component.strip('{}') for component
in subpath.strip('/').split('/') in subpath.strip('/').split('/')
if '{' not in component
] ]
if is_custom_action(action): if is_custom_action(action):

View File

@ -115,13 +115,15 @@ class TestRouterGeneratedSchema(TestCase):
action='get' action='get'
) )
}, },
'read': coreapi.Link( 'id': {
url='/example/{id}/', 'read': coreapi.Link(
action='get', url='/example/{id}/',
fields=[ action='get',
coreapi.Field('id', required=True, location='path', schema=coreschema.String()) fields=[
] coreapi.Field('id', required=True, location='path', schema=coreschema.String())
) ]
)
},
} }
} }
) )
@ -155,24 +157,6 @@ class TestRouterGeneratedSchema(TestCase):
coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B')) coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B'))
] ]
), ),
'read': coreapi.Link(
url='/example/{id}/',
action='get',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
]
),
'custom_action': coreapi.Link(
url='/example/{id}/custom_action/',
action='post',
encoding='application/json',
description='A description of custom action.',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
coreapi.Field('c', required=True, location='form', schema=coreschema.String(title='C')),
coreapi.Field('d', required=False, location='form', schema=coreschema.String(title='D')),
]
),
'custom_list_action': coreapi.Link( 'custom_list_action': coreapi.Link(
url='/example/custom_list_action/', url='/example/custom_list_action/',
action='get' action='get'
@ -187,33 +171,53 @@ class TestRouterGeneratedSchema(TestCase):
action='post' action='post'
) )
}, },
'update': coreapi.Link( 'id': {
url='/example/{id}/', 'read': coreapi.Link(
action='put', url='/example/{id}/',
encoding='application/json', action='get',
fields=[ fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String()), coreapi.Field('id', required=True, location='path', schema=coreschema.String())
coreapi.Field('a', required=True, location='form', schema=coreschema.String(title='A', description=('A field description'))), ]
coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B')) ),
] 'update': coreapi.Link(
), url='/example/{id}/',
'partial_update': coreapi.Link( action='put',
url='/example/{id}/', encoding='application/json',
action='patch', fields=[
encoding='application/json', coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
fields=[ coreapi.Field('a', required=True, location='form', schema=coreschema.String(title='A', description=('A field description'))),
coreapi.Field('id', required=True, location='path', schema=coreschema.String()), coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B'))
coreapi.Field('a', required=False, location='form', schema=coreschema.String(title='A', description='A field description')), ]
coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B')) ),
] 'partial_update': coreapi.Link(
), url='/example/{id}/',
'delete': coreapi.Link( action='patch',
url='/example/{id}/', encoding='application/json',
action='delete', fields=[
fields=[ coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
coreapi.Field('id', required=True, location='path', schema=coreschema.String()) coreapi.Field('a', required=False, location='form', schema=coreschema.String(title='A', description='A field description')),
] coreapi.Field('b', required=False, location='form', schema=coreschema.String(title='B'))
) ]
),
'delete': coreapi.Link(
url='/example/{id}/',
action='delete',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
]
),
'custom_action': coreapi.Link(
url='/example/{id}/custom_action/',
action='post',
encoding='application/json',
description='A description of custom action.',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
coreapi.Field('c', required=True, location='form', schema=coreschema.String(title='C')),
coreapi.Field('d', required=False, location='form', schema=coreschema.String(title='D')),
]
),
}
} }
} }
) )
@ -298,21 +302,23 @@ class TestSchemaGenerator(TestCase):
action='get', action='get',
fields=[] fields=[]
), ),
'read': coreapi.Link( 'id': {
url='/example/{id}/', 'read': coreapi.Link(
action='get', url='/example/{id}/',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
]
),
'sub': {
'list': coreapi.Link(
url='/example/{id}/sub/',
action='get', action='get',
fields=[ fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String()) coreapi.Field('id', required=True, location='path', schema=coreschema.String())
] ]
) ),
'sub': {
'list': coreapi.Link(
url='/example/{id}/sub/',
action='get',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
]
)
}
} }
} }
} }
@ -351,21 +357,64 @@ class TestSchemaGeneratorNotAtRoot(TestCase):
action='get', action='get',
fields=[] fields=[]
), ),
'read': coreapi.Link( 'id': {
url='/api/v1/example/{id}/', 'read': coreapi.Link(
action='get', url='/api/v1/example/{id}/',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
]
),
'sub': {
'list': coreapi.Link(
url='/api/v1/example/{id}/sub/',
action='get', action='get',
fields=[ fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String()) coreapi.Field('id', required=True, location='path', schema=coreschema.String())
] ]
) ),
'sub': {
'list': coreapi.Link(
url='/api/v1/example/{id}/sub/',
action='get',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
]
)
}
}
}
}
)
assert schema == expected
@unittest.skipUnless(coreapi, 'coreapi is not installed')
class TestSchemaGeneratorWithOverlappingKeys(TestCase):
def setUp(self):
self.patterns = [
url('^api/v1/example/(?P<pk>\d+)/?$', ExampleDetailView.as_view()),
url('^api/v1/example/(?P<pk>\d+)/(?P<id2>\d+)/?$', ExampleDetailView.as_view()),
]
def test_schema_for_regular_views(self):
generator = SchemaGenerator(title='Example API', patterns=self.patterns)
schema = generator.get_schema()
expected = coreapi.Document(
url='',
title='Example API',
content={
'example': {
'id': {
'read': coreapi.Link(
url='/api/v1/example/{id}/',
action='get',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String())
]
),
'id2': {
'read': coreapi.Link(
url='/api/v1/example/{id}/{id2}/',
action='get',
fields=[
coreapi.Field('id', required=True, location='path', schema=coreschema.String()),
coreapi.Field('id2', required=True, location='path', schema=coreschema.String())
]
)
}
} }
} }
} }