Skip to content

Commit da92888

Browse files
Partial serializer should not have required fields (#7563)
1 parent 7a9db57 commit da92888

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

rest_framework/schemas/openapi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def map_serializer(self, serializer):
525525
if isinstance(field, serializers.HiddenField):
526526
continue
527527

528-
if field.required:
528+
if field.required and not serializer.partial:
529529
required.append(self.get_field_name(field))
530530

531531
schema = self.map_field(field)

tests/schemas/test_openapi.py

+50
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,56 @@ class View(generics.GenericAPIView):
403403
assert list(schema['properties']['nested']['properties'].keys()) == ['number']
404404
assert schema['properties']['nested']['required'] == ['number']
405405

406+
def test_response_body_partial_serializer(self):
407+
path = '/'
408+
method = 'GET'
409+
410+
class ItemSerializer(serializers.Serializer):
411+
text = serializers.CharField()
412+
413+
def __init__(self, *args, **kwargs):
414+
super().__init__(*args, **kwargs)
415+
self.partial = True
416+
417+
class View(generics.GenericAPIView):
418+
serializer_class = ItemSerializer
419+
420+
view = create_view(
421+
View,
422+
method,
423+
create_request(path),
424+
)
425+
inspector = AutoSchema()
426+
inspector.view = view
427+
428+
responses = inspector.get_responses(path, method)
429+
assert responses == {
430+
'200': {
431+
'description': '',
432+
'content': {
433+
'application/json': {
434+
'schema': {
435+
'type': 'array',
436+
'items': {
437+
'$ref': '#/components/schemas/Item'
438+
},
439+
},
440+
},
441+
},
442+
},
443+
}
444+
components = inspector.get_components(path, method)
445+
assert components == {
446+
'Item': {
447+
'type': 'object',
448+
'properties': {
449+
'text': {
450+
'type': 'string',
451+
},
452+
},
453+
}
454+
}
455+
406456
def test_list_response_body_generation(self):
407457
"""Test that an array schema is returned for list views."""
408458
path = '/'

0 commit comments

Comments
 (0)