drf_openapi icon indicating copy to clipboard operation
drf_openapi copied to clipboard

Proxy label can't be decoded

Open nyssance opened this issue 7 years ago • 5 comments

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

nyssance avatar Dec 21 '17 08:12 nyssance

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 avatar Dec 21 '17 10:12 limdauto

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 ''

jar3b avatar Dec 27 '17 11:12 jar3b

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.

joeribekker avatar Jan 03 '18 16:01 joeribekker

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.

alantrick avatar Jan 05 '18 20:01 alantrick

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.

jaydenwindle avatar Jan 12 '18 19:01 jaydenwindle