DRF - `write_only=True` displays in response schema
Asked Answered
S

1

12

I'm using drf-yasg to document my APIs. However, I'm running into a problem

I've a serializer in which one of the fields is set to write_only=True.

class XYZSerializer(serializers.ModelSerializer):
    status = serializers.BooleanField(default=True, write_only=True)

    class Meta:

        model = XYZ
        fields = ('id', 'status')

When generating the swagger docs, field status still shows in the Response fields. Technically, it should not.

How to correct this?

Servomechanism answered 11/7, 2018 at 12:39 Comment(1)
You should've add link to issue that probably you opened: A field having write_only=True displays in response schema.Blossom
M
2

Shortly: Developers of drf-yasg have answered about it. Problem in OpenAPI 2.0 specification and you could use https://github.com/tfranzel/drf-spectacular (it support OpenAPI 3.0) instead of drf-yasg.

You could in create special serializer for decorator only

@swagger_auto_schema(responses={200: CustomResponseSerializer()})

or make your serilializer's fields dynamic(Django Rest Framework: Dynamically return subset of fields) and decorate action in viewset like this

@swagger_auto_schema(responses={200: YourSerializer(fields=['some_field_name', 'another_...')})

Also in https://github.com/axnsan12/drf-yasg/issues/70 you could find another way from https://github.com/axnsan12/drf-yasg/issues/70#issuecomment-698288806

Mckay answered 14/2, 2021 at 9:43 Comment(1)
Confirmation here. I just dropped drf-yasg for drf-spectacular and it works!Gordan

© 2022 - 2024 — McMap. All rights reserved.