Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DRF - `write_only=True` displays in response schema

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?

like image 323
PythonEnthusiast Avatar asked Jul 11 '18 12:07

PythonEnthusiast


1 Answers

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

like image 157
user11354529 Avatar answered Oct 22 '22 15:10

user11354529