Skip to content

Serializer.get_value returns empty object instead of an instance of the class 'empty' in partial (multipart) updates. #3219

Closed
@elnygren

Description

@elnygren

This is essentially the same problem as with MultiChoiceField in #2993 but now with a Serializer. The fix would also be similar, I believe.

Problem is in the following case, where uploading a file with multipart PATCH can null the related_model relationship.

class SomeSerializer(serializers.Serializer):
  related_model = OtherSerializer()
  file = serializers.FileField()
  ...

The problem is in the following get_value (serializer.py) method that returns a {} instead of empty when doing a multipart PATCH that does not contain the said field (and thus empty would be desired). This could result in accidentally nulling fields (~destroying data) during PATCH requests.

 def get_value(self, dictionary):
    # We override the default field access in order to support
    # nested HTML forms.
    if html.is_html_input(dictionary):
        return html.parse_html_dict(dictionary, prefix=self.field_name)
    return dictionary.get(self.field_name, empty)

The same issue could be present at (they don't do a check for 'partial'):

I noticed this effect using MultiChoiceFields with 3.0.5 but it was fixed in 3.1.3. However, I also bumped into it using a nested Serializer relation like I describe above with SomeSerializer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions