Fix XML UnicodeEncode serialization error

The generic Nova XMLSerializer code will currently attempt
to cast to str the value for all leaf nodes. This patch
ensures that no attempt is made to convert unicode which
can cause a UnicodeEncode error. We don't need to convert
unicode for XML text and regardless we encode to UTF-8 at
a later point.

Change-Id: I8135d2b9a67db62b0eafdd301b7fdb67a5dd72cc
Closes-Bug: #1279172
(cherry picked from commit 53fe869631)
(cherry picked from commit 3136cfc11f)
This commit is contained in:
Chris Yeoh 2014-09-29 16:01:04 +09:30 committed by Matt Riedemann
parent d884d3235b
commit 7cdb643cb3
2 changed files with 11 additions and 1 deletions

View File

@ -435,7 +435,9 @@ class XMLDictSerializer(DictSerializer):
result.appendChild(node)
else:
# Type is atom
node = doc.createTextNode(str(data))
if not isinstance(data, six.string_types):
data = six.text_type(data)
node = doc.createTextNode(data)
result.appendChild(node)
return result

View File

@ -221,6 +221,14 @@ class XMLDictSerializerTest(test.NoDBTestCase):
result = result.replace('\n', '').replace(' ', '')
self.assertEqual(result, expected_xml)
def test_xml_contains_unicode(self):
input_dict = dict(test=u'\u89e3\u7801')
expected_xml = '<test>\xe8\xa7\xa3\xe7\xa0\x81</test>'
serializer = wsgi.XMLDictSerializer()
result = serializer.serialize(input_dict)
result = result.replace('\n', '').replace(' ', '')
self.assertEqual(expected_xml, result)
class JSONDictSerializerTest(test.NoDBTestCase):
def test_json(self):