Skip to content

Commit

Permalink
Fixed crash deleting required schema parameter key on PATCH requests. (
Browse files Browse the repository at this point in the history
  • Loading branch information
guimunarolo authored and Pierre Chiquet committed Mar 24, 2020
1 parent d023684 commit ed92871
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rest_framework/schemas/openapi.py
Expand Up @@ -387,7 +387,7 @@ def _map_serializer(self, serializer):
result = {
'properties': properties
}
if len(required) > 0:
if required:
result['required'] = required

return result
Expand Down Expand Up @@ -463,7 +463,7 @@ def _get_request_body(self, path, method):
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:
Expand Down
25 changes: 25 additions & 0 deletions tests/schemas/test_openapi.py
Expand Up @@ -169,6 +169,31 @@ class View(generics.GenericAPIView):
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'
Expand Down

0 comments on commit ed92871

Please sign in to comment.