Search code examples
pythondjangodjango-rest-frameworkswaggerdrf-yasg

why swagger-UI doesn't show prameters and response?


enter image description here

I'm using django and djangorestframwork and drf-yasg. I have this api in view.py module:

@api_view(http_method_names=["GET"])
def get_all__referralCodes_game(request):

    data = request.query_params
    query = GameCollection.find_one(
        {"_id": ObjectId(data["gameId"]),},
        {"_id": 0,f"referralCodes": 1,}
    )
    if query:
        return Response(data={"msg": "ok", "data": [query["referralCodes"]]}, status=http.HTTPStatus.OK)
    return Response(data={"msg": "not_found", "data": {}}, status=http.HTTPStatus.NOT_FOUND)

and in url.py module I have:

urlpatterns = [
    path("referral-code/get-all", views.get_all__referralCodes_game),
]

I need to have gameId in query params but it can not be seen there.


Solution

  • drf-yasg doesn't know about your gameId query parameter. You need to somehow tell it about the parameter.

    According to the drf-yasg documentation:

    query Parameters - i.e. parameters specified in the URL as /path/?query1=value&query2=value - are generated from your view’s filter_backends and paginator, if any are declared. Additional parameters can be specified via the query_serializer and manual_parameters arguments of @swagger_auto_schema.

    You could use @swagger_auto_schema like this:

    @swagger_auto_schema(
            method='get',
            manual_parameters=[
                openapi.Parameter(
                    'gameId',
                    openapi.IN_QUERY,
                    description='Game ID',
                    required=True,
                    type=openapi.TYPE_INTEGER,
                ),
             ]
    )
    def get_all__referralCodes_game(request):
    
    

    For the response you can either define a serializer (recommended) and set it as the class's serializer_class or you can add a responses value to the @swagger_auto_schema decorator (see the docs for some examples).