Drf-spectacular (swagger) does not show request body of "application/json" in SwaggerUI
I have a login view with the following request body:
However, when I select the content-type "application/json", I only receive the example, without the relevant data from my serializer that is shown in other content-types.
I've tried numerous options to show the request body scheme of the request body, but nothing seems to work.
This is my view:
class LoginSerializer(serializers.Serializer):
username = serializers.CharField(
min_length=4, # Minimum length for login
max_length=50, # Maximum length for login
required=True, # Field is required
help_text="email of the user",
)
password = serializers.CharField(
min_length=8, # Minimum length for password
max_length=128, # Maximum length for password
required=True, # Field is required
style={'input_type': 'password'}, # This will hide the input in browsable API
write_only=True, # Prevents password from being exposed in responses
help_text="password of the user",
)
@extend_schema(examples=[
OpenApiExample(
'Example',
value={"username": "email of the user", "password": "password of the user"},
request_only=True,
)
],)
class LoginView(GenericAPIView):
"""Authenticates the user via django session authentication cookie"""
serializer_class = LoginSerializer
def post(self, request):
serializer = LoginSerializer(data=request.data)
username = request.data.get('username')
password = request.data.get('password')
if not serializer.is_valid():
return ValidationError(serializer.errors)
user = authenticate(username=username, password=password)
if user is None:
return ValidationError('Invalid credentials.')
login(request, user)
return JsonResponse({'detail': 'Successfully logged in.'})
and these are my drf settings:
SPECTACULAR_SETTINGS = {
'TITLE': 'Project API',
'DESCRIPTION': 'Backend Project',
'VERSION': '1.0.0',
'SERVE_INCLUDE_SCHEMA': False,
'SWAGGER_UI_DIST': 'SIDECAR', # shorthand to use the sidecar instead
'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
'REDOC_DIST': 'SIDECAR',
'POSTPROCESSING_HOOKS': [
'drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields', # ensures compatibility with camelizer
],
"SWAGGER_UI_SETTINGS": {
"displayRequestDuration": True
},
# OTHER SETTINGS
}
# DRF Definitions
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
'DEFAULT_RENDERER_CLASSES': [
'djangorestframework_camel_case.render.CamelCaseJSONRenderer',
'djangorestframework_camel_case.render.CamelCaseBrowsableAPIRenderer',
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication', # using Django's Default Session Authentication
],
'DEFAULT_PARSER_CLASSES': [
'djangorestframework_camel_case.parser.CamelCaseFormParser',
'djangorestframework_camel_case.parser.CamelCaseJSONParser',
],
'JSON_UNDERSCOREIZE': {
'no_underscore_before_number': True,
}
}
My goal is to also show the request body information for JSON requests. How could one achieve this?