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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With