Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix parsing multipart data using a nested serializer with list#3820

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@lrnt
Copy link
Contributor

@lrntlrnt commentedJan 11, 2016
edited by lovelydinosaur
Loading

Parsing multipart data using a nested serializer with a list can result in incorrect behavior. If the list contains more than one item, only the last item will be saved.

Here is an example:

classNestedSerializer(serializers.Serializer):example=serializers.MultipleChoiceField(choices=[1,2,3])classTestSerializer(serializers.Serializer):nested=NestedSerializer()

When you send the following data formatted as JSON everything will be fine:

{'nested': {'example': [1,2],    }}#   --->{'nested': {'example': [1,2],    }}

However, when you send the same data formatted as multipart data the serialized object will only take the last element of the list:

{'nested.example': [1,2]}#   --->{'nested' {'example':2,    }}

The bug is situated inparse_html_dict(dictionary, prefix='') inrest_framework.utils.html. This function takes and returns a newMultiValueDict. A key in aMultiValueDict can have multiple values, this is how a list is represented. When accessing a key in aMultiValueDict only the last element of the list is returned.

This can be easily solved by using .getlist() and .setlist() when getting and setting the value of theMultiValueDict.

Closes#3710.

TestNestedSerializerWithList checks if a list (a MulitpleChoiceField inthis case) in a nested serializer is correctly parsed from json ormultipart formatted data.
It is possible that a key in a MultiValueDict has multiple values, listsare represented this way. When accessing a key in a MultiValueDictit only returns the last element of that key. This becomes a problemwhen parsing an html dict with a list inside of it.To fix this problem we have to get and set the value using .getlist()and .setlist().This commit solves a bug that caused a list in a nested serializer tonot being properly parsed if it was sent as multipart formated data.
@lovelydinosaur
Copy link
Contributor

Yup, looks good. Thanks!

lrnt reacted with thumbs up emoji

@lovelydinosaurlovelydinosaur merged commitfdde44d intoencode:masterJun 23, 2016
@lrntlrnt deleted the fix-nested-lists-multipart branchJune 24, 2016 08:38
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

Projects

None yet

Milestone

3.4.0 Release

Development

Successfully merging this pull request may close these issues.

3 participants

@lrnt@lovelydinosaur@xordoquy

[8]ページ先頭

©2009-2025 Movatter.jp