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:
@@ -81,6 +81,8 @@ class Selector(object):
|
||||
if callable(elem):
|
||||
obj = elem(obj)
|
||||
else:
|
||||
if obj == '':
|
||||
return ''
|
||||
# Use indexing
|
||||
try:
|
||||
obj = obj[elem]
|
||||
|
@@ -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):
|
||||
|
Reference in New Issue
Block a user