mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
Partial serializer should not have required fields (#7563)
This commit is contained in:
parent
7a9db57eaf
commit
da9288878b
|
@ -525,7 +525,7 @@ class AutoSchema(ViewInspector):
|
|||
if isinstance(field, serializers.HiddenField):
|
||||
continue
|
||||
|
||||
if field.required:
|
||||
if field.required and not serializer.partial:
|
||||
required.append(self.get_field_name(field))
|
||||
|
||||
schema = self.map_field(field)
|
||||
|
|
|
@ -403,6 +403,56 @@ class TestOperationIntrospection(TestCase):
|
|||
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 = '/'
|
||||
|
|
Loading…
Reference in New Issue
Block a user