drf-yasg
drf-yasg copied to clipboard
Cannot remove endpoint from swagger
Hi,
I am having trouble hiding some endpoints from the swagger UI.
Environment:
- Python 3.6.0
- djangorestframework==3.9.4
- drf-yasg==1.16.1
(Btw, I found a dependency that is not listed in the install instructions: packaging. Took me forever to debug it...)
I have a simple endpoint I would like to remove from swagger:
@throttle_classes([TwicePerSecondUserThrottle])
class InvoicesView(APIView):
def get(self, request, **kwargs):
"""
Returns a list of card for the user
* Requires token authentication.
"""
.......
return Response(status=status.HTTP_200_OK, data=invoices)
I tried:
@throttle_classes([TwicePerSecondUserThrottle])
class InvoicesView(APIView):
swagger_schema = None
def get(self, request, **kwargs):
....
And
@throttle_classes([TwicePerSecondUserThrottle])
@swagger_auto_schema(method='get', swagger_schema=None)
class InvoicesView(APIView):
swagger_schema = None
def get(self, request, **kwargs):
"""
Returns a list of card for the user
* Requires token authentication.
"""
-> AssertionError: method or methods can only be specified on @action or @api_view views' (Same error with list instead of get
@throttle_classes([TwicePerSecondUserThrottle])
class InvoicesView(APIView):
@swagger_auto_schema(swagger_schema=None)
def get(self, request, **kwargs):
"""
Returns a list of card for the user
* Requires token authentication.
"""
And still no luck. Anyone could help me?
My solution:
- Add a variable to settings.py. For example:
DRF_YASG_EXCLUDE_VIEWS = [
'rest_auth.views.LoginView',
]
Then create a custom classes that inherits EndpointEnumerator and OpenAPISchemaGenerator:
from django.conf import settings
from drf_yasg.generators import OpenAPISchemaGenerator, EndpointEnumerator
class CustomEndpointEnumerator(EndpointEnumerator):
"""
Add custom setting to exclude views
"""
def should_include_endpoint(self, path, callback, app_name='',
namespace='', url_name=None):
view = '{}.{}'.format(callback.__module__, callback.__qualname__)
if view in settings.DRF_YASG_EXCLUDE_VIEWS:
return False
return super().should_include_endpoint(
path, callback, app_name, namespace, url_name
)
class CustomOpenAPISchemaGenerator(OpenAPISchemaGenerator):
"""
We want change default endpoint enumerator class
"""
endpoint_enumerator_class = CustomEndpointEnumerator
And urls.py:
from .yasg import CustomOpenAPISchemaGenerator
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(title='Docs', default_version='v1'),
generator_class=CustomOpenAPISchemaGenerator,
)
I know this issue is old but try out what i mentioned in #596