Fixed crash deleting required schema parameter key on PATCH requests. (#6944)

Closes #6941
This commit is contained in:
Guilherme Munarolo 2019-10-10 03:50:20 -03:00 committed by Carlton Gibson
parent 9850441e6f
commit 0fd72f17ee
2 changed files with 27 additions and 2 deletions

View File

@ -387,7 +387,7 @@ class AutoSchema(ViewInspector):
result = {
'properties': properties
}
if len(required) > 0:
if required:
result['required'] = required
return result
@ -463,7 +463,7 @@ class AutoSchema(ViewInspector):
content = self._map_serializer(serializer)
# No required fields for PATCH
if method == 'PATCH':
del content['required']
content.pop('required', None)
# No read_only fields for request.
for name, schema in content['properties'].copy().items():
if 'readOnly' in schema:

View File

@ -169,6 +169,31 @@ class TestOperationIntrospection(TestCase):
for response in inspector._get_responses(path, method).values():
assert 'required' not in response['content']['application/json']['schema']
def test_empty_required_with_patch_method(self):
path = '/'
method = 'PATCH'
class Serializer(serializers.Serializer):
read_only = serializers.CharField(read_only=True)
write_only = serializers.CharField(write_only=True, required=False)
class View(generics.GenericAPIView):
serializer_class = Serializer
view = create_view(
View,
method,
create_request(path)
)
inspector = AutoSchema()
inspector.view = view
request_body = inspector._get_request_body(path, method)
# there should be no empty 'required' property, see #6834
assert 'required' not in request_body['content']['application/json']['schema']
for response in inspector._get_responses(path, method).values():
assert 'required' not in response['content']['application/json']['schema']
def test_response_body_generation(self):
path = '/'
method = 'POST'