DRF Swagger – Endpoint parameter doesn't match the Serializer

Question:

So I’m trying to create at REST API using DRF and Swagger for API Documentation, But I notice that Swagger UI Parameter doesn’t match the given Serializer.
MailSerializer.py
`

from main.BusinessLayer.Model.Mails import Mails
from rest_framework import serializers


class MailSerializer(serializers.Serializer):
    class Meta:
        model = Mails
        fields = '__all__'

    # Mail Properties
    Subject =  serializers.CharField(max_length = 30, allow_blank = False)
    Sender = serializers.CharField(max_length = 30, allow_blank = False)
    Recipients = serializers.CharField(allow_blank = False)
    ReplyToAddress = serializers.CharField(max_length = 30)
    Importance = serializers.CharField(max_length = 30, allow_blank = False)
    ApplicationId = serializers.CharField(max_length = 30, allow_blank = False)

    # Mail Content
    Body = serializers.CharField(allow_blank = False)
    Attachments = serializers.CharField(allow_blank = False)

    # Other Parameters
    UseDefaultHeader = serializers.IntegerField(default = 0)
    UseDefaultFooter = serializers.IntegerField(default = 0)

    def create(self, validated_data):
        return Mails.objects.create(**validated_data)

`

 @swagger_auto_schema(
        operation_description="", 
        operation_summary="create a mail",
        request_body=MailSerializer
    )
    def create(self, request, *args, **kwargs):
        try:
            serializer = self.serializer_class(data=request.data)
            serializer.is_valid(raise_exception=True)
            self.perform_create(serializer)
            
            return Response({
                            'code': 200,
                            'success': True, 
                            'message': '%s successfully created.' % (OBJECT_NAME),
                            'data': []
                            }, status= status.HTTP_200_OK)

enter image description here

I tried to use @swagger_auto_schema and a request_body parameters, but still not working

Asked By: Marlon Bucalan

||

Answers:

The problem may be in using Meta.fields with non-model serializer, try changing this:

class MailSerializer(serializers.Serializer):

to:

class MailSerializer(serializers.ModelSerializer):
Answered By: Ersain

allow_blank set to true means that the empty string should be considered a valid value. That doesn’t mean it isn’t required.

So try required, default value is True, so set required=False

Answered By: ilyasbbu
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.