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

Does not work with multipart upload #82

Open
serendipity1004 opened this issue Apr 10, 2020 · 11 comments
Open

Does not work with multipart upload #82

serendipity1004 opened this issue Apr 10, 2020 · 11 comments

Comments

@serendipity1004
Copy link

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
Copy link
Owner

vbabiy commented Jun 16, 2020

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

@banagale
Copy link

banagale commented Nov 14, 2020

@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
Copy link

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

@tremlab
Copy link

tremlab commented Sep 22, 2021

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
Copy link
Owner

vbabiy commented Sep 23, 2021

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

@banagale
Copy link

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

@krzysieqq
Copy link

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
Copy link

tremlab commented Sep 23, 2021

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
Copy link

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
Copy link

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
Copy link

eugene-babichenko commented Feb 17, 2024

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.

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

6 participants