diff --git a/drf_spectacular/contrib/rest_framework_simplejwt.py b/drf_spectacular/contrib/rest_framework_simplejwt.py index 64dda317..04165367 100644 --- a/drf_spectacular/contrib/rest_framework_simplejwt.py +++ b/drf_spectacular/contrib/rest_framework_simplejwt.py @@ -75,7 +75,7 @@ def get_security_definition(self, auto_schema): else: if header_name.startswith('HTTP_'): header_name = header_name[5:] - header_name = header_name.capitalize() + header_name = header_name.replace('_', '-').capitalize() return { 'type': 'apiKey', 'in': 'header', diff --git a/tests/contrib/test_simplejwt.py b/tests/contrib/test_simplejwt.py index fcfd45c8..62019a06 100644 --- a/tests/contrib/test_simplejwt.py +++ b/tests/contrib/test_simplejwt.py @@ -62,3 +62,17 @@ def test_simplejwt_non_bearer_keyword(no_warnings): 'description': 'Token-based authentication with required prefix "JWT"' } } + + +@pytest.mark.contrib('rest_framework_simplejwt') +@mock.patch('rest_framework_simplejwt.settings.api_settings.AUTH_HEADER_NAME', 'HTTP_X_TOKEN') +def test_simplejwt_non_std_header_name(no_warnings): + schema = generate_schema('/x', XViewset) + assert schema['components']['securitySchemes'] == { + 'jwtAuth': { + 'type': 'apiKey', + 'in': 'header', + 'name': 'X-token', + 'description': 'Token-based authentication with required prefix "Bearer"' + } + }