Inline Serializer for Request Body Not Displaying in DRF Spectacular Documentation
I am working on a Django REST Framework (DRF) project using DRF Spectacular for API documentation. My ViewSet
does not use a serializer because the logic does not require one, but I need to describe the request body shape in the API documentation.
To achieve this, I am using DRF Spectacular's inline_serializer
to define the request body schema dynamically. Here’s an example of how I’ve implemented it:
from drf_spectacular.utils import extend_schema, inline_serializer
from rest_framework import serializers
from drf_spectacular.types import OpenApiTypes
from rest_framework.response import Response
class ExampleViewSet(viewsets.ViewSet):
@extend_schema(
request=inline_serializer(
name="InlineFormSerializer",
fields={
"str_field": serializers.CharField(),
"int_field": serializers.IntegerField(),
"file_field": serializers.FileField(),
},
),
responses={200: OpenApiTypes.OBJECT}, # Placeholder for actual response schema
)
def create(self, request, *args, **kwargs):
# Business logic here
return Response({"message": "Success"})
While the schema generation works without errors, the request body schema described via inline_serializer
does not appear in the frontend API documentation (e.g., Swagger UI). The rest of the API is documented correctly.
Steps I've Taken
- Confirmed that DRF Spectacular is installed and properly configured in settings.py
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',
}
- Verified that the schema generation command runs without issues
- Confirmed that inline_serializer is being used as per the documentation.
- Ensured the swagger-ui is accessible and displays other endpoints correctly.
What could be causing the inline_serializer
request schema to not display in the frontend documentation? Are there any additional configurations or best practices I might be missing for documenting API endpoints that do not use DRF serializers?
I would appreciate any insights or recommendations for resolving this issue