I was wondering is there anyway to add an Example Object to the Response Object? I also see it is in the openapi.py.
I looked through the code and when I manually set Responses it's generate an Open API response at https://github.com/axnsan12/drf-yasg/blob/master/src/drf_yasg/inspectors/view.py#L234
Is there anyway to get it to set the example? If not I am willing to figure out how to get it working.
Hello,
You can set the examples field (and any other field) by directly passing a Response object instead of a serializer. Reading the documentation for swagger_auto_schema is a good start. The testproj folder also has an example of doing this.
For latecomers, this StackOverflow post probably will help you to customize the response and the response example.
_Originally posted by @jerinpetergeorge in https://github.com/axnsan12/drf-yasg/issues/214#issuecomment-700413258_
Hi @axnsan12 , I couldn't find the code that sets examples in the testproj, can you point me to it?
Also, I am setting examples this way:
post_response_schema = {
status.HTTP_201_CREATED: openapi.Response(
description="User successfully created",
examples={
"application/json": {}
}
),
ERROR_STATUS[EXPECTED_KEY_NOT_PRESENT_IN_REQUEST]: openapi.Response(
description="Key error: Key not present",
examples={
"application/json": {
"error": EXPECTED_KEY_NOT_PRESENT_IN_REQUEST.format("email not present")
}
}
),
}
@swagger_auto_schema(responses=post_response_schema)
def post
....
My redoc documentation shows the examples fine. But, my swagger just shows a loading spinner.
my swagger just shows a loading spinner.
Did you mean _this spinner_ ? @divya1c

If so, click on Example Value and then you will be able to see the example data.
Oh that works. That's weird though. Why doesn't it show by default? @jerinpetergeorge
Just CSS things :rofl:
@jerinpetergeorge thx for your help btw do you know how to resolve loading spinner bug?
Unfortunately, Nop @lieric7766
Hi everyone! First of all I want to thank you, this is working :)
Is there any way to put a serializer as a response instead of manually creating all the possible responses? Maybe @jerinpetergeorge or @lieric7766 can help me with this.
I think the loading spinner shouldn't be a difficult thing to solve, if I do so I'll create a PR.
Is there any way to put a serializer as a response instead of manually creating all the possible responses? Maybe @jerinpetergeorge or @lieric7766 can help me with this.
I think there is no way to do that. But, I wonder how would that serializer look like?
I have a ViewSet @action (that internally do many things and that is not mapped to any entity) so the serializer of the POST method looks like:
{
"field_1": "field_1_content",
"field_2": "field_2_content",
"field_3": "field_3_content",
}
Then, if everything goes well I would like to return as a response two different serialized objects. Something like
{
"object_A": {
"x": "field_1_content",
"y": "field_2_content"
},
"object_B": {
"z": "field_3_content"
}
}
The problem is that now it's only showing the serializer of the POST as the response object.
Have you tried to set request_body and responses options of **@swagger_auto_schema(...)?
@swagger_auto_schema(
request_body=RequestSerializer,
responses={"200": ResponseSerializer}
)
@ifranco14 Did you try sthg like:
from rest_framework import serializers
class ObjectSerializer(serializers.Serializer):
x = serializers.CharField()
y = serializers.CharField()
In your ViewSet
data_response = openapi.Response('Data response', ObjectSerializer)
@swagger_auto_schema(
method='post',
responses={200: data_response}
)
Have you tried to set
request_bodyandresponsesoptions of **@swagger_auto_schema(...)?@swagger_auto_schema( request_body=RequestSerializer, responses={"200": ResponseSerializer} )
This one worked! Thanks a lot @jerinpetergeorge and @Khaledjallouli!
Most helpful comment
Hello,
You can set the
examplesfield (and any other field) by directly passing aResponseobject instead of a serializer. Reading the documentation forswagger_auto_schemais a good start. Thetestprojfolder also has an example of doing this.