django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

DictField parsing for "html" inputs misleadingly returns an empty dict even if the field was not specified

Open brett-lempereur opened this issue 6 years ago • 0 comments

Checklist

  • [*] I have verified that that issue exists against the master branch of Django REST framework.
  • [*] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [*] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [*] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [*] I have reduced the issue to the simplest possible case.
  • [-] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

Using a DictField and custom partial update logic in a serializer, a condition such as:

if not self.partial or "field-name" in data:

Is always true when the request type is multipart/form-data, even if the field was not specified. In fact, in this case, it seems like it is impossible to distinguish between an empty input and an unspecified input for that field.

Expected behavior

The condition if not self.partial or "field-name" in data should only be true when the field was specified by the client, even when using multipart/form-data requests.

Actual behavior

The condition is always true for mutlipart/form-data requests, even when the field was not specified as part of the request, and the update method has no way to distinguish between an empty input and an unspecified field.

Note that this is not the case for ListField, and the problem seems to be in the return of the function at https://github.com/encode/django-rest-framework/blob/master/rest_framework/utils/html.py#L69. The equivalent function for a list field returns an empty value if no matching elements were found in the input. For the dict field parsing logic, an empty dictionary is incorrectly returned.

brett-lempereur avatar Oct 07 '18 16:10 brett-lempereur