How to use serializer in custom OpenApiResponse?
mglowinski93 opened this issue · 4 comments
mglowinski93 commented
Hi,
I would like to use a decorator extend_schema
to overwrite responses.
Below solution worked great.
@swagger_utils.extend_schema(
responses={
status.HTTP_200_OK: LocationSerializer,
status.HTTP_404_NOT_FOUND: swagger_utils.OpenApiResponse(
description="Location with specified ID does not exist.",
response={
"type": "object",
"properties": {
"detail": {"type": "string", "example": "Not found."}
},
},
),
},
)
What in case I would like to include serializer in manually defined response like below?
@swagger_utils.extend_schema(
responses={
status.HTTP_200_OK: swagger_utils.OpenApiResponse(
response={
"type": "array",
"properties": LocationSerializer(many=True),
}
)
}
)
tfranzel commented
Hi, something like this I would say:
@extend_schema(
responses=OpenApiResponse(
swagger_utils.inline_serializer(
name="SomeName",
fields={"properties": LocationSerializer(many=True)},
many=True
)
)
)
mglowinski93 commented
The issue is, that I need to add also additional information at the same level, to describe response like this:
{
"count": XXX,
"results": Serializer data
}
With upper proposition, I can only display data from defined serializer, but pagination data are missing.
tfranzel commented
I don't really understand what you are asking for exactly. Maybe this: https://drf-spectacular.readthedocs.io/en/latest/faq.html#how-do-i-wrap-my-responses-my-endpoints-are-wrapped-in-a-generic-envelope
mglowinski93 commented
Indeed, thank you!