tfranzel/drf-spectacular

How to use serializer in custom OpenApiResponse?

mglowinski93 opened this issue · 4 comments

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),
            }
        )
    }
)

Hi, something like this I would say:

    @extend_schema(
        responses=OpenApiResponse(
            swagger_utils.inline_serializer(
                name="SomeName",
                fields={"properties": LocationSerializer(many=True)},
                many=True
            )
        )
    )

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.

Indeed, thank you!