Added openapi_tags for group by tags as openapi specification

This commit is contained in:
bergran 2020-02-04 00:37:47 +01:00
parent 4137ef41ef
commit 8dfa39085c
3 changed files with 72 additions and 0 deletions

View File

@ -87,6 +87,7 @@ class AutoSchema(ViewInspector):
operation['operationId'] = self._get_operation_id(path, method)
operation['description'] = self.get_description(path, method)
operation['tags'] = self._get_tags(path, method)
parameters = []
parameters += self._get_path_parameters(path, method)
@ -211,6 +212,24 @@ class AutoSchema(ViewInspector):
return paginator.get_schema_operation_parameters(view)
def _get_tags(self, path, method):
"""
Get tags parameters from view
Default value to return it will be tuple with default tag
In other hand it will try to get it from openapi_tags of view
"""
tags = ['default']
if hasattr(self.view, 'openapi_tags'):
tags = getattr(self.view, 'openapi_tags', tags)
assert type(tags) == list, 'openapi_tags property is not a list'
assert len(tags) > 0, 'openapi_tags property should has almost one tag'
return tags
def _map_choicefield(self, field):
choices = list(OrderedDict.fromkeys(field.choices)) # preserve order and remove duplicates
if all(isinstance(choice, bool) for choice in choices):

View File

@ -125,6 +125,7 @@ class TestOperationIntrospection(TestCase):
assert operation == {
'operationId': 'listDocStringExamples',
'description': 'A description of my GET operation.',
'tags': ['default'],
'parameters': [],
'responses': {
'200': {
@ -157,6 +158,47 @@ class TestOperationIntrospection(TestCase):
assert operation == {
'operationId': 'RetrieveDocStringExampleDetail',
'description': 'A description of my GET operation.',
'tags': ['default'],
'parameters': [{
'description': '',
'in': 'path',
'name': 'id',
'required': True,
'schema': {
'type': 'string',
},
}],
'responses': {
'200': {
'description': '',
'content': {
'application/json': {
'schema': {
},
},
},
},
},
}
def test_modify_openapi_tags(self):
path = '/example/{id}/'
method = 'GET'
view = create_view(
views.DocStringExampleDetailWithTagsView,
method,
create_request(path)
)
inspector = AutoSchema()
inspector.view = view
operation = inspector.get_operation(path, method)
assert operation == {
'operationId': 'RetrieveDocStringExampleDetailWithTags',
'description': 'A description of my GET operation.',
'tags': ['DocString'],
'parameters': [{
'description': '',
'in': 'path',

View File

@ -53,6 +53,17 @@ class DocStringExampleDetailView(APIView):
pass
class DocStringExampleDetailWithTagsView(APIView):
permission_classes = [permissions.IsAuthenticatedOrReadOnly]
openapi_tags = ['DocString']
def get(self, *args, **kwargs):
"""
A description of my GET operation.
"""
pass
# Generics.
class ExampleSerializer(serializers.Serializer):
date = serializers.DateField()