From d7bb1eaa350a1f38599f481540225cb26078162e Mon Sep 17 00:00:00 2001 From: Denis Orehovsky Date: Wed, 30 Sep 2020 08:39:18 +0300 Subject: [PATCH] Partial serializer should not have required fields --- rest_framework/schemas/openapi.py | 2 +- tests/schemas/test_openapi.py | 50 +++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/rest_framework/schemas/openapi.py b/rest_framework/schemas/openapi.py index 8a8e267e0f..a7fd91c2ec 100644 --- a/rest_framework/schemas/openapi.py +++ b/rest_framework/schemas/openapi.py @@ -515,7 +515,7 @@ def map_serializer(self, serializer): if isinstance(field, serializers.HiddenField): continue - if field.required: + if field.required and not serializer.partial: required.append(field.field_name) schema = self.map_field(field) diff --git a/tests/schemas/test_openapi.py b/tests/schemas/test_openapi.py index 8ea910dc1e..b8ca241107 100644 --- a/tests/schemas/test_openapi.py +++ b/tests/schemas/test_openapi.py @@ -349,6 +349,56 @@ class View(generics.GenericAPIView): assert list(schema['properties']['nested']['properties'].keys()) == ['number'] assert schema['properties']['nested']['required'] == ['number'] + def test_response_body_partial_serializer(self): + path = '/' + method = 'GET' + + class ItemSerializer(serializers.Serializer): + text = serializers.CharField() + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.partial = True + + class View(generics.GenericAPIView): + serializer_class = ItemSerializer + + view = create_view( + View, + method, + create_request(path), + ) + inspector = AutoSchema() + inspector.view = view + + responses = inspector.get_responses(path, method) + assert responses == { + '200': { + 'description': '', + 'content': { + 'application/json': { + 'schema': { + 'type': 'array', + 'items': { + '$ref': '#/components/schemas/Item' + }, + }, + }, + }, + }, + } + components = inspector.get_components(path, method) + assert components == { + 'Item': { + 'type': 'object', + 'properties': { + 'text': { + 'type': 'string', + }, + }, + } + } + def test_list_response_body_generation(self): """Test that an array schema is returned for list views.""" path = '/'