Skip to content

Commit

Permalink
Merge pull request #803 from tfranzel/obtainauthtoken
Browse files Browse the repository at this point in the history
mitigate DRF bug in ObtainAuthToken < 3.12.0 #796
  • Loading branch information
tfranzel authored Aug 30, 2022
2 parents 2a4c298 + e19bf9c commit d2cc9cb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions drf_spectacular/contrib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'django_oauth_toolkit',
'djangorestframework_camel_case',
'rest_auth',
'rest_framework',
'rest_polymorphic',
'rest_framework_dataclasses',
'rest_framework_jwt',
Expand Down
32 changes: 32 additions & 0 deletions drf_spectacular/contrib/rest_framework.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from drf_spectacular.extensions import OpenApiViewExtension


class ObtainAuthTokenView(OpenApiViewExtension):
target_class = 'rest_framework.authtoken.views.ObtainAuthToken'
match_subclasses = True

def view_replacement(self):
"""
Prior to DRF 3.12.0, usage of ObtainAuthToken resulted in AssertionError
Incompatible AutoSchema used on View "ObtainAuthToken". Is DRF's DEFAULT_SCHEMA_CLASS ...
This is because DRF had a bug which made it NOT honor DEFAULT_SCHEMA_CLASS and instead
injected an unsolicited coreschema class for this view and this view only. This extension
fixes the view before the wrong schema class is used.
Bug in DRF that was fixed in later versions:
https://github.com/encode/django-rest-framework/blob/4121b01b912668c049b26194a9a107c27a332429/rest_framework/authtoken/views.py#L16
"""
from rest_framework import VERSION

from drf_spectacular.openapi import AutoSchema

# no intervention needed
if VERSION >= '3.12':
return self.target

class FixedObtainAuthToken(self.target):
schema = AutoSchema()

return FixedObtainAuthToken
6 changes: 6 additions & 0 deletions tests/test_regressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2912,3 +2912,9 @@ class XViewset(viewsets.ModelViewSet):
queryset = SimpleModel.objects.all()

generate_schema('/x', XViewset)


def test_drf_authtoken_schema_override_bug(no_warnings):
from rest_framework.authtoken.views import ObtainAuthToken

generate_schema('/token/', view=ObtainAuthToken)

0 comments on commit d2cc9cb

Please # to comment.