Home > Enterprise >  How to set required fields in PATCH API in Swagger UI
How to set required fields in PATCH API in Swagger UI

Time:08-03

I'm using drf-spectacular and here's code in settings.py

SPECTACULAR_SETTINGS = {
    'TITLE': 'TITLE',
    'VERSION': '1.0.0',
    'SCHEMA_PATH_PREFIX_TRIM': True,
    'PREPROCESSING_HOOKS': ["custom.url_remover.preprocessing_filter_spec"],  
}

in serializers.py

class ChangePasswordSerilaizer(serializers.Serializer):
    current_password = serializers.CharField(write_only=True, min_length=8, required=True)
    new_password = serializers.CharField(write_only=True, min_length=8, required=True)
    confirm_new_password = serializers.CharField(write_only=True, min_length=8, required=True)

but still fields in request body are showing not required

enter image description here

CodePudding user response:

change your SPECTACULAR_SETTINGS

    SPECTACULAR_SETTINGS = {
        'TITLE': 'APP NAME',
        'VERSION': '1.0.0',
        'SCHEMA_PATH_PREFIX_TRIM': True,
        'PREPROCESSING_HOOKS': ["custom.url_remover.preprocessing_filter_spec"], 
        'COMPONENT_SPLIT_PATCH': False, 
    }

by default COMPONENT_SPLIT_PATCH is true in SPECTACULAR_SETTINGS so you can simply override('COMPONENT_SPLIT_PATCH': False) it to fix this problem

  • Related