Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
6 tasks done
alexcreid opened this issue Apr 27, 2016 · 2 comments
Open
6 tasks done
Labels

Comments

@alexcreid
Copy link

alexcreid commented Apr 27, 2016

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

alexcreid added a commit to alexcreid/django-rest-framework that referenced this issue Apr 27, 2016
@tomchristie
Copy link
Member

Confirmed - thanks for raising the issue.

Using the following...

class Nested(serializers.Serializer):
    b = serializers.IntegerField()
    c = serializers.IntegerField()


class ExampleSerializer(serializers.Serializer):
    a = Nested()

    def validate_a(self, data):
        raise serializers.ValidationError('Oh no')

class ExampleView(GenericAPIView):
    serializer_class = ExampleSerializer

    def get(self, request):
        return Response()

    def post(self, request):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        return Response()

Submitting the form results in...

image

@tomchristie tomchristie added the Bug label Jun 1, 2016
@tomchristie tomchristie added this to the 3.4.0 Release milestone Jun 1, 2016
@tomchristie tomchristie changed the title Errors from parent-level validation of nested serializer fields breaks the form-like API Errors from parent-level validation of nested serializer fields break the browsable API. Jun 1, 2016
@carltongibson
Copy link
Collaborator

I'm going to de-milestone this for now.

@carltongibson carltongibson removed this from the 3.7.0 Release milestone Oct 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants