10

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?

Praful Bagai
  • 16,684
  • 50
  • 136
  • 267
  • You should've add link to issue that *probably you* opened: [*A field having `write_only=True` displays in response schema.*](https://github.com/axnsan12/drf-yasg/issues/165) – vishes_shell Sep 23 '18 at 21:27

1 Answers1

1

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

rrretry
  • 103
  • 1
  • 7