Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Proxy label can't be decoded #105

Open
nyssance opened this issue Dec 21, 2017 · 5 comments · May be fixed by #111
Open

Proxy label can't be decoded #105

nyssance opened this issue Dec 21, 2017 · 5 comments · May be fixed by #111

Comments

@nyssance
Copy link

nyssance commented Dec 21, 2017

File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/drf_openapi/entities.py", line 298, in fallback_schema_from_field
title = force_text(field.label) if field.label else ''
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/coreapi/compat.py", line 50, in force_text
return string.decode('utf-8')
AttributeError: 'proxy' object has no attribute 'decode'

python3 str no decode

@limdauto
Copy link
Owner

limdauto commented Dec 21, 2017

That's unfortunate. Let me check it.

Upon closer investigation, @nypisces there is already a check in place to make sure what we are decoding isn't str. In your stacktrace, it says proxy object. Would be helpful if you can pinpoint which field it is crashing on because it's not a string. Maybe a translation object?

@limdauto limdauto changed the title 1.2.0 crash on python3 Non-string label can't be decoded Dec 21, 2017
@limdauto limdauto changed the title Non-string label can't be decoded Proxy label can't be decoded Dec 21, 2017
@jar3b
Copy link

jar3b commented Dec 27, 2017

Hello, @limdauto, i have same issue with field "password" in custom django user model.

Last exception line is:
AttributeError: '__proxy__' object has no attribute 'decode'

And value of label is (and you are right, it is a translation object):

label = {__proxy__} Пароль
 _delegate_bytes = {bool} False
 _delegate_text = {bool} True
 _proxy____args = {tuple} <class 'tuple'>: ('пароль',)
 _proxy____kw = {dict} {}
 _proxy____prepared = {bool} True

You can reproduce this issue by setting LANGUAGE_CODE = 'ru-ru', with en-us all works well.

converting __proxy__ to str also works good, for example:

        def ext_force_text(obj):
            return str(obj) if obj.__class__.__name__ == '__proxy__' else force_text(obj)

        title = ext_force_text(field.label) if field.label else ''
        description = ext_force_text(field.help_text) if field.help_text else ''

@joeribekker
Copy link

Sure, this happens because the model fields use ugettext or ugettext_lazy. This library could use Django's own force_text function which can handle these "strings" correctly.

@alantrick
Copy link

I made the change that joeribekker suggested and it works. I haven't tested it on all the different platforms though, so I didn't make a pull request.

@jaydenwindle
Copy link

jaydenwindle commented Jan 12, 2018

I ran into this issue on a project, and used @alantrick's edit to fix it (Thanks! 😄). Throwing up a PR to try and get this in quickly so we can stop depending on a fork.

joeribekker added a commit to maykinmedia/drf_openapi that referenced this issue Jan 24, 2018
joeribekker added a commit to maykinmedia/drf_openapi that referenced this issue Jan 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants