Tags override support (#259)

master
Vitali Fokin 2018-12-11 21:13:37 +03:00 committed by Cristi Vîjdea
parent c52daaea8c
commit f415a96aa6
5 changed files with 27 additions and 13 deletions

View File

@ -70,6 +70,8 @@ You want to contribute some code? Great! Here are a few steps to get you started
# (optional) sort imports with isort and check flake8 linting # (optional) sort imports with isort and check flake8 linting
(venv) $ isort --apply (venv) $ isort --apply
(venv) $ flake8 src/drf_yasg testproj tests setup.py (venv) $ flake8 src/drf_yasg testproj tests setup.py
# install test dependencies
(venv) $ pip install -U -r requirements/test.txt
# run tests in the current environment, faster than tox # run tests in the current environment, faster than tox
(venv) $ pytest --cov (venv) $ pytest --cov
# (optional) run tests for other python versions in separate environments # (optional) run tests for other python versions in separate environments

View File

@ -376,13 +376,18 @@ class SwaggerAutoSchema(ViewInspector):
def get_tags(self, operation_keys): def get_tags(self, operation_keys):
"""Get a list of tags for this operation. Tags determine how operations relate with each other, and in the UI """Get a list of tags for this operation. Tags determine how operations relate with each other, and in the UI
each tag will show as a group containing the operations that use it. each tag will show as a group containing the operations that use it. If not provided in overrides,
tags will be inferred from the operation url.
:param tuple[str] operation_keys: an array of keys derived from the pathdescribing the hierarchical layout :param tuple[str] operation_keys: an array of keys derived from the pathdescribing the hierarchical layout
of this view in the API; e.g. ``('snippets', 'list')``, ``('snippets', 'retrieve')``, etc. of this view in the API; e.g. ``('snippets', 'list')``, ``('snippets', 'retrieve')``, etc.
:rtype: list[str] :rtype: list[str]
""" """
return [operation_keys[0]] tags = self.overrides.get('tags')
if not tags:
tags = [operation_keys[0]]
return tags
def get_consumes(self): def get_consumes(self):
"""Return the MIME types this endpoint can consume. """Return the MIME types this endpoint can consume.

View File

@ -29,7 +29,7 @@ class unset(object):
def swagger_auto_schema(method=None, methods=None, auto_schema=unset, request_body=None, query_serializer=None, def swagger_auto_schema(method=None, methods=None, auto_schema=unset, request_body=None, query_serializer=None,
manual_parameters=None, operation_id=None, operation_description=None, operation_summary=None, manual_parameters=None, operation_id=None, operation_description=None, operation_summary=None,
security=None, deprecated=None, responses=None, field_inspectors=None, filter_inspectors=None, security=None, deprecated=None, responses=None, field_inspectors=None, filter_inspectors=None,
paginator_inspectors=None, **extra_overrides): paginator_inspectors=None, tags=None, **extra_overrides):
"""Decorate a view method to customize the :class:`.Operation` object generated from it. """Decorate a view method to customize the :class:`.Operation` object generated from it.
`method` and `methods` are mutually exclusive and must only be present when decorating a view method that accepts `method` and `methods` are mutually exclusive and must only be present when decorating a view method that accepts
@ -96,6 +96,7 @@ def swagger_auto_schema(method=None, methods=None, auto_schema=unset, request_bo
:attr:`.ViewInspector.filter_inspectors` on the :class:`.inspectors.SwaggerAutoSchema` instance :attr:`.ViewInspector.filter_inspectors` on the :class:`.inspectors.SwaggerAutoSchema` instance
:param list[.PaginatorInspector] paginator_inspectors: extra paginator inspectors; these will be tried before :param list[.PaginatorInspector] paginator_inspectors: extra paginator inspectors; these will be tried before
:attr:`.ViewInspector.paginator_inspectors` on the :class:`.inspectors.SwaggerAutoSchema` instance :attr:`.ViewInspector.paginator_inspectors` on the :class:`.inspectors.SwaggerAutoSchema` instance
:param list[str] tags: tags override
:param extra_overrides: extra values that will be saved into the ``overrides`` dict; these values will be available :param extra_overrides: extra values that will be saved into the ``overrides`` dict; these values will be available
in the handling :class:`.inspectors.SwaggerAutoSchema` instance via ``self.overrides`` in the handling :class:`.inspectors.SwaggerAutoSchema` instance via ``self.overrides``
""" """
@ -115,6 +116,7 @@ def swagger_auto_schema(method=None, methods=None, auto_schema=unset, request_bo
'filter_inspectors': list(filter_inspectors) if filter_inspectors else None, 'filter_inspectors': list(filter_inspectors) if filter_inspectors else None,
'paginator_inspectors': list(paginator_inspectors) if paginator_inspectors else None, 'paginator_inspectors': list(paginator_inspectors) if paginator_inspectors else None,
'field_inspectors': list(field_inspectors) if field_inspectors else None, 'field_inspectors': list(field_inspectors) if field_inspectors else None,
'tags': list(tags) if tags else None,
} }
data = filter_none(data) data = filter_none(data)
if auto_schema is not unset: if auto_schema is not unset:

View File

@ -13,7 +13,11 @@ from users.serializers import UserListQuerySerializer, UserSerializerrr
class UserList(APIView): class UserList(APIView):
"""UserList cbv classdoc""" """UserList cbv classdoc"""
@swagger_auto_schema(query_serializer=UserListQuerySerializer, responses={200: UserSerializerrr(many=True)}) @swagger_auto_schema(
query_serializer=UserListQuerySerializer,
responses={200: UserSerializerrr(many=True)},
tags=['Users'],
)
def get(self, request): def get(self, request):
queryset = User.objects.all() queryset = User.objects.all()
serializer = UserSerializerrr(queryset, many=True) serializer = UserSerializerrr(queryset, many=True)
@ -28,7 +32,8 @@ class UserList(APIView):
'username': openapi.Schema(type=openapi.TYPE_STRING) 'username': openapi.Schema(type=openapi.TYPE_STRING)
}, },
), ),
security=[] security=[],
tags=['Users'],
) )
def post(self, request): def post(self, request):
serializer = UserSerializerrr(request.data) serializer = UserSerializerrr(request.data)
@ -36,17 +41,17 @@ class UserList(APIView):
serializer.save() serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.data, status=status.HTTP_201_CREATED)
@swagger_auto_schema(operation_id="users_dummy", operation_description="dummy operation") @swagger_auto_schema(operation_id="users_dummy", operation_description="dummy operation", tags=['Users'])
def patch(self, request): def patch(self, request):
pass pass
@swagger_auto_schema(method='put', request_body=UserSerializerrr) @swagger_auto_schema(method='put', request_body=UserSerializerrr, tags=['Users'])
@swagger_auto_schema(methods=['get'], manual_parameters=[ @swagger_auto_schema(methods=['get'], manual_parameters=[
openapi.Parameter('test', openapi.IN_QUERY, "test manual param", type=openapi.TYPE_BOOLEAN), openapi.Parameter('test', openapi.IN_QUERY, "test manual param", type=openapi.TYPE_BOOLEAN),
], responses={ ], responses={
200: openapi.Response('response description', UserSerializerrr), 200: openapi.Response('response description', UserSerializerrr),
}) }, tags=['Users'])
@api_view(['GET', 'PUT']) @api_view(['GET', 'PUT'])
def user_detail(request, pk): def user_detail(request, pk):
"""user_detail fbv docstring""" """user_detail fbv docstring"""

View File

@ -761,7 +761,7 @@ paths:
items: items:
$ref: '#/definitions/UserSerializerrr' $ref: '#/definitions/UserSerializerrr'
tags: tags:
- users - Users
post: post:
operationId: users_create operationId: users_create
description: apiview post description override description: apiview post description override
@ -787,7 +787,7 @@ paths:
username: username:
type: string type: string
tags: tags:
- users - Users
security: [] security: []
patch: patch:
operationId: users_dummy operationId: users_dummy
@ -797,7 +797,7 @@ paths:
'200': '200':
description: '' description: ''
tags: tags:
- users - Users
parameters: [] parameters: []
/users/{id}/: /users/{id}/:
get: get:
@ -814,7 +814,7 @@ paths:
schema: schema:
$ref: '#/definitions/UserSerializerrr' $ref: '#/definitions/UserSerializerrr'
tags: tags:
- users - Users
put: put:
operationId: users_update operationId: users_update
description: user_detail fbv docstring description: user_detail fbv docstring
@ -830,7 +830,7 @@ paths:
schema: schema:
$ref: '#/definitions/UserSerializerrr' $ref: '#/definitions/UserSerializerrr'
tags: tags:
- users - Users
parameters: parameters:
- name: id - name: id
in: path in: path