Skip to content

Commit 0fd72f1

Browse files
guimunarolocarltongibson
authored andcommitted
Fixed crash deleting required schema parameter key on PATCH requests. (#6944)
Closes #6941
1 parent 9850441 commit 0fd72f1

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

rest_framework/schemas/openapi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def _map_serializer(self, serializer):
387387
result = {
388388
'properties': properties
389389
}
390-
if len(required) > 0:
390+
if required:
391391
result['required'] = required
392392

393393
return result
@@ -463,7 +463,7 @@ def _get_request_body(self, path, method):
463463
content = self._map_serializer(serializer)
464464
# No required fields for PATCH
465465
if method == 'PATCH':
466-
del content['required']
466+
content.pop('required', None)
467467
# No read_only fields for request.
468468
for name, schema in content['properties'].copy().items():
469469
if 'readOnly' in schema:

tests/schemas/test_openapi.py

+25
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,31 @@ class View(generics.GenericAPIView):
169169
for response in inspector._get_responses(path, method).values():
170170
assert 'required' not in response['content']['application/json']['schema']
171171

172+
def test_empty_required_with_patch_method(self):
173+
path = '/'
174+
method = 'PATCH'
175+
176+
class Serializer(serializers.Serializer):
177+
read_only = serializers.CharField(read_only=True)
178+
write_only = serializers.CharField(write_only=True, required=False)
179+
180+
class View(generics.GenericAPIView):
181+
serializer_class = Serializer
182+
183+
view = create_view(
184+
View,
185+
method,
186+
create_request(path)
187+
)
188+
inspector = AutoSchema()
189+
inspector.view = view
190+
191+
request_body = inspector._get_request_body(path, method)
192+
# there should be no empty 'required' property, see #6834
193+
assert 'required' not in request_body['content']['application/json']['schema']
194+
for response in inspector._get_responses(path, method).values():
195+
assert 'required' not in response['content']['application/json']['schema']
196+
172197
def test_response_body_generation(self):
173198
path = '/'
174199
method = 'POST'

0 commit comments

Comments
 (0)