KeyError: 'required' in openapi.py · Issue #6941 · encode/django-rest-framework (original) (raw)

Checklist

Steps to reproduce

Expected behavior

Docs are generated

Actual behavior

KeyError: 'required'

rest_framework.schemas.openapi.AutoSchema._get_request_body has code:

        # No required fields for PATCH
        if method == 'PATCH':
            del content['required']

But key required is generated by rest_framework.schemas.openapi.AutoSchema._map_serializer:

        if len(required) > 0:
            result['required'] = required

So if there is no required fields in serializer KeyError will be raised. I think _get_request_body should handle deletion in this way:

        # No required fields for PATCH
        if method == 'PATCH and 'required' in content:
            del content['required']