Skip to content

Fixed ListField empty check which ignored multi-part list elements #4763

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

Closed
Closed
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
9 changes: 6 additions & 3 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1521,12 +1521,15 @@ def __init__(self, *args, **kwargs):
self.child.bind(field_name='', parent=self)

def get_value(self, dictionary):
if self.field_name not in dictionary:
if getattr(self.root, 'partial', False):
return empty
# We override the default field access in order to support
# lists in HTML forms.
if html.is_html_input(dictionary):
if getattr(self.root, 'partial', False):
if self.field_name not in dictionary:
regexp = re.compile(html.HTML_LIST_REGEXP_FORMAT % re.escape(self.field_name))
if not any(regexp.match(key) for key in dictionary.keys()):
return empty

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not comfortable moving the empty check entirely inside is_html_input - seems like we might be missing some failure cases that introduces that aren't covered by our test cases.

Any way around we could arrange this PR that has a smaller change footprint?

val = dictionary.getlist(self.field_name, [])
if len(val) > 0:
# Support QueryDict lists in HTML input.
Expand Down
5 changes: 4 additions & 1 deletion rest_framework/utils/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from django.utils.datastructures import MultiValueDict


HTML_LIST_REGEXP_FORMAT = r'^%s\[([0-9]+)\](.*)$'


def is_html_input(dictionary):
# MultiDict type datastructures are used to represent HTML form input,
# which may have more than one value for each key.
Expand Down Expand Up @@ -46,7 +49,7 @@ def parse_html_list(dictionary, prefix=''):
]
"""
ret = {}
regex = re.compile(r'^%s\[([0-9]+)\](.*)$' % re.escape(prefix))
regex = re.compile(HTML_LIST_REGEXP_FORMAT % re.escape(prefix))
for field, value in dictionary.items():
match = regex.match(field)
if not match:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,24 @@ class TestSerializer(serializers.Serializer):
assert serializer.is_valid()
assert serializer.validated_data == {'scores': [1]}

def test_querydict_indexed_list_input(self):
class TestSerializer(serializers.Serializer):
scores = serializers.ListField(child=serializers.IntegerField())

instance = {'scores': []}
serializer = TestSerializer(instance=instance, data=QueryDict('scores[0]=1&scores[1]=3'), partial=True)
assert serializer.is_valid()
assert serializer.validated_data == {'scores': [1, 3]}

def test_querydict_indexed_list_input_only_one_input(self):
class TestSerializer(serializers.Serializer):
scores = serializers.ListField(child=serializers.IntegerField())

instance = {'scores': []}
serializer = TestSerializer(instance=instance, data=QueryDict('scores[0]=1'), partial=True)
assert serializer.is_valid()
assert serializer.validated_data == {'scores': [1]}


class TestCreateOnlyDefault:
def setup(self):
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