encode / django-rest-framework

Web APIs for Django. 🎸
https://www.django-rest-framework.org
Other
28.45k stars 6.85k forks source link

is_api_view check only one level of inheritance #9592

Closed ya-pekatoros closed 3 hours ago

ya-pekatoros commented 4 hours ago

Hi!

I've faced one issue connected with schema generation.

Case:

I want to release some common logic for some of my views, for instance: token check. One of the way to do it without repetitions is just:

from rest_framework.permissions import BasePermission
from rest_framework.views import APIView

class SecretTokenPermission(BasePermission):
    pass

class BaseInternalApiView(APIView):
    permission_classes = [SecretTokenPermission]

But then when I use something like:

class AccountTopUpView(BaseInternalApiView):
    def get(self, request):
        pass

is_api_view check in rest_framework/schemas/openapi.py return False because AccountTopUpView is not a subclass of APIView:

def is_api_view(callback):
    """
    Return `True` if the given view callback is a REST framework view/viewset.
    """
    # Avoid import cycle on APIView
    from rest_framework.views import APIView
    cls = getattr(callback, 'cls', None)
    return (cls is not None) and issubclass(cls, APIView)

I want to suggest this change: let's check the full path of inheritance.

ya-pekatoros commented 3 hours ago

Sry the problem was in public=False parameter in get_schema_view