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

Errors from parent-level validation of nested serializer fields break the browsable API.

Open alexcreid opened this issue 8 years ago • 2 comments

Checklist

  • [x] I have verified that that issue exists against the master branch of Django REST framework.
  • [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [x] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [x] 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.)
  • [x] I have reduced the issue to the simplest possible case.
  • [x] 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

(See PR #4074 for a test that reproduces this.)

  1. Create a serialiser that contains a nested serialiser as one of its fields.
  2. Add validation for the nested serialiser field in the top-level serialiser (for example, as a validate_<field name> method.
  3. Instantiate the serialiser with data that will cause the validate_<field name> method to fail.
  4. Retrieve the NestedBoundField for the nested serialiser using the serialiser's form-like api (<serializer instance>[<field name>])
  5. Attempt to retrieve a BoundField for one of the nested serialiser's fields from the NestedBoundField instance.

Expected behaviour

A BoundField is returned.

Actual behaviour

An AttributeError is raised, as the NestedBoundField attempts to treat its errors as a dictionary, even though they are the list of error messages from the parent serialiser's validation error. This impacts the browsable API.

(It's not entirely clear to me what the behaviour should be where a nested serialiser has a list of errors rather than a dictionary, but it probably shouldn't be to error out.)

alexcreid avatar Apr 27 '16 12:04 alexcreid