Error 400 raises TypeError: expected string or buffer
H0neyBadger opened this issue · comments
Hello,
I often get the following error when my API is supposed to raises an error 400.
2016-02-29 16:25:22,765 [ERROR] django.request.handle_uncaught_exception: Internal Server Error: /api/v1/users/
Traceback (most recent call last):
File "/home/user/lib/python3.4/site-packages/django/core/handlers/base.py", line 174, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/user/lib/python3.4/site-packages/django/core/handlers/base.py", line 172, in get_response
response = response.render()
File "/home/user/lib/python3.4/site-packages/django/template/response.py", line 160, in render
self.content = self.rendered_content
File "/home/user/lib/python3.4/site-packages/rest_framework/response.py", line 71, in rendered_content
ret = renderer.render(self.data, media_type, context)
File "/home/user/lib/python3.4/site-packages/rest_framework_xml/renderers.py", line 37, in render
self._to_xml(xml, data)
File "/home/user/lib/python3.4/site-packages/rest_framework_xml/renderers.py", line 53, in _to_xml
self._to_xml(xml, value)
File "/home/user/lib/python3.4/site-packages/rest_framework_xml/renderers.py", line 47, in _to_xml
self._to_xml(xml, item)
File "/home/user/lib/python3.4/site-packages/rest_framework_xml/renderers.py", line 61, in _to_xml
xml.characters(smart_text(data))
File "/home/user/lib/python3.4/site-packages/django/utils/xmlutils.py", line 24, in characters
if content and re.search(r'[\x00-\x08\x0B-\x0C\x0E-\x1F]', content):
File "/home/user/lib/python3.4/re.py", line 166, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or buffer
In the debug report, I can see the following value:
content = <django.utils.functional.lazy.<locals>.__proxy__ object at 0x7fb564172828>
Did I miss something in my DRF configuration?
djangorestframework-xml==1.3.0
djangorestframework==3.3.2
@H0neyBadger - sorry for the super delayed response here. I just want to check in with you if you remember if and how this was resolved on your end and if this has anything to do with django-rest-framework-xml
?
@kyleobrien91 @H0neyBadger - same error here. we use django.utils.translation.ugettext_lazy. ugettext_lazy translates the strings in a lazy fashion return <class 'django.utils.functional.lazy.<locals>.__proxy__'> is a Promise
. In the XMLRenderer._to_xml method django-rest-framework-xml
use django.utils.encoding.smart_text
if we look more closely this method
if isinstance(s, Promise):
# The input is the result of a gettext_lazy() call.
return s
So smart_text will never return a string in this case.
(Pdb) type(value)
<class 'django.utils.functional.lazy.<locals>.__proxy__'>
(Pdb) type(smart_text(value))
<class 'django.utils.functional.lazy.<locals>.__proxy__'>
(Pdb) isinstance(value, Promise)
True
Temporary fix: override the _to_xml() method and use django.utils.encoding.force_text
instead of django.utils.encoding.smart_text
(Pdb) type(force_text(value))
<class 'str'>
I hope this has answered your question if this has anything to do with django-rest-framework-xml ?
I have already created a fork. But I am now 2 weeks on vacation.