7

How can I add example responses -- (openapi doc) to my swagger doc using drf-yasg package?

JPG
  • 82,442
  • 19
  • 127
  • 206
TNN
  • 391
  • 1
  • 5
  • 12

2 Answers2

16

Use drf_yasg.openapi.Response--(drf-yasg doc) with the help of @swagger_auto_schema(...)--(drf-yasg doc) decorator as

from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from rest_framework.response import Response
from rest_framework.views import APIView

response_schema_dict = {
    "200": openapi.Response(
        description="custom 200 description",
        examples={
            "application/json": {
                "200_key1": "200_value_1",
                "200_key2": "200_value_2",
            }
        }
    ),
    "205": openapi.Response(
        description="custom 205 description",
        examples={
            "application/json": {
                "205_key1": "205_value_1",
                "205_key2": "205_value_2",
            }
        }
    ),
}


class MyTestAPIView(APIView):

    @swagger_auto_schema(responses=response_schema_dict)
    def post(self, request, *args, **kwargs):
        return Response({"foo": "bar"})

Schema rendered Result

Schema rendered Result

Update

its keep loading and not showing anything

You may need to click on "Example Value" text if you are looking in Swagger doc

loading spinner

JPG
  • 82,442
  • 19
  • 127
  • 206
  • So I have to write custom response for every api ? I have 22 apis . so for all of them ? – TNN Sep 29 '20 at 09:57
  • 1
    Yes, unless any of them are returning the same response. – JPG Sep 29 '20 at 10:03
  • Sir , Can You tell how I can add post parameter in it as well ? I can't send post parameter – TNN Sep 30 '20 at 21:32
  • This is an *"another question"*, please do ask a new question regarding the new issue. BTW, please do accept and upvote the answer if you found this answer was helpful. – JPG Oct 01 '20 at 03:08
  • Then there must be some configuration issue in your case. This answer will work as-is @Fawad – JPG Oct 07 '20 at 10:49
  • In my case too, the examples is just loading and not showing anything. – Divya Konda Oct 29 '20 at 05:47
  • Can you add a [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example) to reproduce the issue? @DivyaKonda – JPG Oct 29 '20 at 05:59
  • @ArakkalAbu I posted more details as an answer. the comments seem to have a character limit – Divya Konda Oct 29 '20 at 06:20
  • 1
    I expected a *new question* with a reference link @DivyaKonda – JPG Oct 29 '20 at 06:22
  • someone actually answered on git - https://github.com/axnsan12/drf-yasg/issues/86 seems to be a css issue. – Divya Konda Oct 29 '20 at 06:26
  • @FawadRana I hope the update might be useful for you!!! – JPG Oct 29 '20 at 06:33
2

In response to @JPG's response, there is a quick fix for that. Go to settings and add this.

SWAGGER_SETTINGS = {
    "DEFAULT_MODEL_RENDERING": "example"
}

This will render the example first.

Farooq
  • 31
  • 1