Skip to content

Enforce allow_empty=False during partial validation of parent serializer #6512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,6 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.child.bind(field_name='', parent=self)

def bind(self, field_name, parent):
super().bind(field_name, parent)
self.partial = self.parent.partial

def get_initial(self):
if hasattr(self, 'initial_data'):
return self.to_representation(self.initial_data)
Expand Down Expand Up @@ -647,9 +643,6 @@ def to_internal_value(self, data):
}, code='not_a_list')

if not self.allow_empty and len(data) == 0:
if self.parent and self.partial:
raise SkipField()

message = self.error_messages['empty']
raise ValidationError({
api_settings.NON_FIELD_ERRORS_KEY: [message]
Expand Down
45 changes: 45 additions & 0 deletions tests/test_serializer_lists.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pytest
from django.http import QueryDict
from django.utils.datastructures import MultiValueDict

from rest_framework import serializers
from rest_framework.exceptions import ErrorDetail


class BasicObject:
Expand Down Expand Up @@ -223,6 +225,49 @@ def test_validate_html_input(self):
assert serializer.validated_data == expected_output


class TestNestedListSerializerAllowEmpty:
"""Tests the behaviour of allow_empty=False when a ListSerializer is used as a field."""

@pytest.mark.parametrize('partial', (False, True))
def test_allow_empty_true(self, partial):
"""
If allow_empty is True, empty lists should be allowed regardless of the value
of partial on the parent serializer.
"""
class ChildSerializer(serializers.Serializer):
id = serializers.IntegerField()

class ParentSerializer(serializers.Serializer):
ids = ChildSerializer(many=True, allow_empty=True)

serializer = ParentSerializer(data={'ids': []}, partial=partial)
assert serializer.is_valid()
assert serializer.validated_data == {
'ids': [],
}

@pytest.mark.parametrize('partial', (False, True))
def test_allow_empty_false(self, partial):
"""
If allow_empty is False, empty lists should fail validation regardless of the value
of partial on the parent serializer.
"""
class ChildSerializer(serializers.Serializer):
id = serializers.IntegerField()

class ParentSerializer(serializers.Serializer):
ids = ChildSerializer(many=True, allow_empty=False)

serializer = ParentSerializer(data={'ids': []}, partial=partial)
assert not serializer.is_valid()
assert serializer.errors == {
'ids': {
'non_field_errors': [
ErrorDetail(string='This list may not be empty.', code='empty')],
}
}


class TestNestedListOfListsSerializer:
def setup(self):
class TestSerializer(serializers.Serializer):
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy