Fix empty selector XML bug

The XML serialization fails when it's trying to create a datum selector with no
values. In that case the obj is an empty string so the selection of datum from
it just fails.
Instead of raising an exception the fix is returning an empty string, that
allows to serialize Selector with an empty object.

Fixes bug 1223358

Change-Id: If5d564c9b037eb0a107f4688ffa1aceb5281dc42
This commit is contained in:
Andrea Rosa
2013-09-11 16:24:09 +01:00
parent 28741163f4
commit 902184f5da
2 changed files with 28 additions and 0 deletions

View File

@@ -81,6 +81,8 @@ class Selector(object):
if callable(elem):
obj = elem(obj)
else:
if obj == '':
return ''
# Use indexing
try:
obj = obj[elem]

View File

@@ -724,6 +724,32 @@ class TemplateTest(test.NoDBTestCase):
result = master.serialize(obj)
self.assertEqual(expected_xml, result)
def test__serialize_with_empty_datum_selector(self):
# Our test object to serialize
obj = {
'test': {
'name': 'foobar',
'image': ''
},
}
root = xmlutil.TemplateElement('test', selector='test',
name='name')
master = xmlutil.MasterTemplate(root, 1)
root_slave = xmlutil.TemplateElement('test', selector='test')
image = xmlutil.SubTemplateElement(root_slave, 'image',
selector='image')
image.set('id')
xmlutil.make_links(image, 'links')
slave = xmlutil.SlaveTemplate(root_slave, 1)
master.attach(slave)
siblings = master._siblings()
result = master._serialize(None, obj, siblings)
self.assertEqual(result.tag, 'test')
self.assertEqual(result[0].tag, 'image')
self.assertEqual(result[0].get('id'), str(obj['test']['image']))
class MasterTemplateBuilder(xmlutil.TemplateBuilder):
def construct(self):