Skip to content

Commit

Permalink
mitigate DRF bug in ObtainAuthToken < 3.12.0 #796
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Aug 30, 2022
1 parent 842b71e commit 4f226b0
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 @@ -2886,3 +2886,9 @@ def view_func(request, format=None):
'type': 'string',
'format': 'regex'
}


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 4f226b0

Please sign in to comment.