vbabiy / djangorestframework-camel-case

Camel case JSON support for Django REST framework.
Other
635 stars 122 forks source link

Does not work with multipart upload #82

Open serendipity1004 opened 4 years ago

serendipity1004 commented 4 years ago

I am using restframework 3.11.0, camel case 1.1.2 and unfortunately it doesn't seem to work for Multipart files when I add MultipartParser like below

class JobPostViewSet(viewsets.ModelViewSet):
    parser_classes = (parsers.MultiPartParser,)

I have to send the data in snake case for my api to work when I am using MultipartParser

vbabiy commented 4 years ago

@serendipity1004 can you test the latest version, if not PR with at least a test would be really helpful.

banagale commented 3 years ago

@vbabiy First, this is a really great package. Its nice to have the automated sanity of the correct casing on frontend and backend. I don't know how people do DRF without it, (it should be built into DRF!). So thank you for your work on it.

I noticed that it seems to not work on ImageField as well.

I'm running Django 3.1.3, DRF 3.12.2 and DRF-C-C 1.2.0.

In my case the frontend would prepare the POST data like:

    const getInputData = async () => {
      let input = document.getElementById('profile-photo-input');
      const formData = new FormData();
      data.append('profile_photo', input.files[0]);
      return formData;
    };

And then the serializer that takes it looks like:

class UserProfilePhotoSerializer(serializers.Serializer):  # noqa
    profile_photo = serializers.ImageField(required=True)

Trying to send up camel case from the frontend failed, so I have to use the snake case as per the line above data.append('profile_photo')...

I'd be happy to try and provide more useful info, if you can clarify what could be added that would be useful I'll try and get it in this issue or a PR.

krzysieqq commented 3 years ago

Try to use PUT instead of POST method in my case it helped.

tremlab commented 3 years ago

Having the same issue. 😢 Has anyone else found a solution? I see there are a few other similar packages out there...has anyone tried them? https://pypi.org/search/?q=djangorestframework-camel-case

Unfortunately I can't switch from POST to PUT, but do you have any idea what exactly is broken about the POST? @krzysieqq

Thanks!

vbabiy commented 3 years ago

If anyone can help me with a test case, I can try to get this fixed.

banagale commented 3 years ago

I’ll try to find the project this was happening on.

krzysieqq commented 3 years ago

I think that the problem is a little more complicated. I was trying to debug this but I didn't find any fast fix. The problem could start in Django middlewares, and it seems that one of them, opens the file in stream buffer before it goes to this package. When a request goes into renderer it doesn't come into parse function because it stops earlier. More precisely it stops in rest_framework/requests.py:321 _parse() function, requests falls into first try/except then go into 336 if which return True, and return values in 337 line.

@vbabiy I'll try to write test in few hours/days

tremlab commented 3 years ago

It is very complicated! I tried to follow the code but still don’t understand what is happening. @krzysieqq

For me, the root failure happened here:

…/lib/python3.7/site-packages/rest_framework/fields.py

def to_internal_value(self, data):: https://github.com/vbabiy/djangorestframework-camel-case/releases
    try:
        # `UploadedFile` objects should have name and size attributes.
        file_name = data.name
        file_size = data.size
    except AttributeError:
        self.fail('invalid')

The function expects the data argument to be a file, but in my case it is a LIST containing one file. So file.name blows up. No idea how this is connected to the camel_case package. 🤷‍♀️🤣

The bug does not occur in djangorestframework-camel-case v0.2.0. I see the error when trying to upgrade to v1.2.0. User above also reported the issue in v1.1.2. I can’t quite follow what is going on with the releases here: https://github.com/vbabiy/djangorestframework-camel-case/releases but - I have everything queued up locally. If someone could give me a list of the versions between these, I can try to bissect which exact version introduced the bug (if that helps.)

I’m using:

python 3.7.10 djangorestframework==3.8.1

I want to second this @banagale : “First, this is a really great package. Its nice to have the automated sanity of the correct casing on frontend and backend. I don't know how people do DRF without it, (it should be built into DRF!). So thank you for your work on it.” And also thanks for responding so quickly!!!!! 🙏 @vbabiy

krzysieqq commented 3 years ago

It's funny but I'm unable to recreate bug at new project. I also try to recreate in another project i which i had this problem, but i also I can't. I'll later try to recreate maybe

banagale commented 3 years ago

It's funny but I'm unable to recreate bug at new project. I also try to recreate in another project i which i had this problem, but i also I can't. I'll later try to recreate maybe

I think this is what happened for me! I ran into an issue, reported it here, then I think it uh, “went away.” I don’t remember fixing it but more like, “maybe I was mistaken about this being broken.”

I was going to mention that in my most recent reply, but it didn’t seem like that did much to stand up my previous report of the issue.

I would have wanted to have provided some follow-up that it was intermittent or something more descriptive than leaving it as though it happened and no work-around was found.

eugene-babichenko commented 7 months ago

I encountered this problem myself. Turns out, when explicitly specifying parser_classes you have to use CamelCaseMultiPartParser instead of DRF's MultiPartParser. That's it, that's the problem. Very easy to miss, very hard to spot.