drf-yasg icon indicating copy to clipboard operation
drf-yasg copied to clipboard

Serializer to openapi.Schema

Open zak10 opened this issue 5 years ago • 3 comments

First off, awesome library!

Running into an issue with some custom ModelViewSet methods not showing anything in their responses. To deal with it, I added a method decorator:

@method_decorator(
    name="list",
    decorator=swagger_auto_schema(
        operation_id="List Questions",
        operation_description="List Questions ",
        responses={"200": openapi.Response("OK", QuestionSerializer(many=True)},
        security=[{"JWT": []}, {None: []}],
    ),
)

but it is unable to recognize that this should be wrapped in a PageNumberPagination result.

I tried manually creating the payload by modifying the decorator:

@method_decorator(
    name="list",
    decorator=swagger_auto_schema(
        operation_id="List Questions",
        operation_description="List Questions",
        responses={"200": openapi.Response("OK", openapi.Schema(
                type=openapi.TYPE_OBJECT,
                properties=OrderedDict((
                    ('count', openapi.Schema(type=openapi.TYPE_INTEGER)),
                    ('next', openapi.Schema(type=openapi.TYPE_STRING, format=openapi.FORMAT_URI, x_nullable=True)),
                    ('previous', openapi.Schema(type=openapi.TYPE_STRING, format=openapi.FORMAT_URI, x_nullable=True)),
                    ('results', openapi.Schema(type=openapi.TYPE_ARRAY, items=openapi.Response("OK", QuestionSerializer(many=True)))),
                )),
                required=['results']
            ))},
        security=[{"JWT": []}, {None: []}],
    ),
)

but obviously this doesn't work. I tried a few things in the "results" property, but couldn't generate a schema from the serializer. I saw another issue mentioning this but had trouble following along to a resolution.

is there any way to do this?

zak10 avatar Jun 07 '19 20:06 zak10