I have the following problem, which has been difficult for me to find documentation on function-based views to document them with drf_spectacular
since all this is found for class-based views
I have tried different ways to document the requestBody
in swagger using serializers_class
, manual objects, inline_serializers
but all this without any success, here I leave a fragment of my code and a screenshot of how it should be and where should I be
@extend_schema(
summary="Fetch order information",
description="This endpoint filter orders based on the provided parameters.",
tags=["orders"],
request=OrderInfoParamsSerializer, # HERE IS MY PROBLEM
responses={
200: OrderInfoSerializer(many=True),
},
)
@api_view(["GET"])
@token_required
def order_info(request: Request) -> Response:
params = OrderInfoParamsSerializer(data=request.data)
if not params.is_valid():
return Response({"error": params.errors}, status=status.HTTP_400_BAD_REQUEST)
params = params.validated_data
...
class OrderInfoParamsSerializer(serializers.Serializer):
order_id = serializers.IntegerField(
required=False, allow_null=True, help_text="Filter by order ID"
)
class OrderInfoSerializer(serializers.ModelSerializer):
class Meta:
model = Orders
fields = "__all__"
I tried to migrate the views to those based on classes where it only worked with one in the POST method not with the GET (I could migrate all my views if this worked)
I didn't want to have to refactor the entire project and would like to be able to find some solution that works for function-based views.
Apparently, since the GET method does not have a request body according to the HTTP standard, drf-spectacular
does not recognize it.
Only unsafe methods can have a requestBody
https://github.com/tfranzel/drf-spectacular/issues/809
The request body applicable for this operation. The requestBody is fully supported in HTTP methods where the HTTP 1.1 specification RFC7231 has explicitly defined semantics for request bodies. In other cases where the HTTP spec is vague (such as GET, HEAD and DELETE), requestBody is permitted but does not have well-defined semantics and SHOULD be avoided if possible.