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

POST list data breaks all Views / ViewSets #5637

Closed
5 of 6 tasks
FeroxTL opened this issue Nov 30, 2017 · 4 comments
Closed
5 of 6 tasks

POST list data breaks all Views / ViewSets #5637

FeroxTL opened this issue Nov 30, 2017 · 4 comments

Comments

@FeroxTL
Copy link

FeroxTL commented Nov 30, 2017

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

List data makes any view / viewset crash.

I tested versions from 3.6.3 till 3.7.3 (current pypi) and master.

Steps to reproduce

Just create simple ApiView / Viewset and serializer (does not matter Serializer or ModelSerializer)

from rest_framework import generics, serializers

class TestSerializer(serializers.Serializer):
    value = serializers.IntegerField()

class TestView(generics.CreateAPIView):
    serializer_class = TestSerializer

    def get_queryset(self):
        return TestModel.objects.all()

then post JSON data (any array):
[{}]

You get stack trace:

Internal Server Error: /api/v1/test/
Traceback (most recent call last):
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 217, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 215, in _get_response
    response = response.render()
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/django/template/response.py", line 107, in render
    self.content = self.rendered_content
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/rest_framework/response.py", line 72, in rendered_content
    ret = renderer.render(self.data, accepted_media_type, context)
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/rest_framework/renderers.py", line 716, in render
    context = self.get_context(data, accepted_media_type, renderer_context)
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/rest_framework/renderers.py", line 689, in get_context
    'post_form': self.get_rendered_html_form(data, view, 'POST', request),
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/rest_framework/renderers.py", line 495, in get_rendered_html_form
    return self.render_form_for_serializer(existing_serializer)
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/rest_framework/renderers.py", line 521, in render_form_for_serializer
    serializer.data,
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/rest_framework/serializers.py", line 533, in data
    ret = super(Serializer, self).data
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/rest_framework/serializers.py", line 266, in data
    self._data = self.get_initial()
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/rest_framework/serializers.py", line 403, in get_initial
    for field_name, field in self.fields.items()
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/rest_framework/serializers.py", line 404, in <listcomp>
    if (field.get_value(self.initial_data) is not empty) and
  File "/slowhome/ferox/Projects/ubp/myenv/lib/python3.5/site-packages/rest_framework/fields.py", line 433, in get_value
    return dictionary.get(self.field_name, empty)

Expected behavior

Validation input data type?

Actual behavior

500

@FeroxTL FeroxTL changed the title POST empty list data breaks all Views / ViewSets POST list data breaks all Views / ViewSets Nov 30, 2017
@tomchristie
Copy link
Member

If anyone's looking through the issues and wants to contribute... first thing we need to do here is see if someone else can reproduce and confirm the issue.

@carltongibson
Copy link
Collaborator

carltongibson commented Dec 1, 2017

@FeroxTL This isn't specific enough to be actionable at the moment.

  • What's the actual exception? You put the traceback by what's the error?
  • render_form_for_serializer — So this is for the Browsable API right? What happens for a JSON request?
  • Can you put together a test project that demonstrates this? — It clearly does not apply to just any view or viewset: posting your exact body to an example endpoint give me the expected response:
{
    "non_field_errors": [
        "Invalid data. Expected a dictionary, but got list."
    ]
}

So we need more to be able to reproduce this. (Most likely there's an error elsewhere in your code, but without the exception it's impossible to say...)

I'm going to close this as is. Happy to re-open if we can identify a concrete issue.

@FeroxTL
Copy link
Author

FeroxTL commented Dec 1, 2017

@carltongibson As far as I can see this bug exists in render_form_for_serializer that is used only in Browsable API. So when it gets a list instead of dict is fails.

If Browsable API is turned off (for example

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer',
    ]
...

) this would not be affected. By default it is on.

You need to activate Browsable API (if it is off) and try again. I have created repo with simple test, that shows this bug — https://github.com/FeroxTL/rest_test
Just follow readme.md

@rpkilby
Copy link
Member

rpkilby commented Dec 3, 2017

Thanks @FeroxTL. I'm reopening this, given the reproduction/test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants