Skip to content

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

Open
@alexcreid

Description

@alexcreid

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

(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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions