Properly handle overridden Body properties

The Resource._get_mapping method was previously overwriting mapping
entries as it traversed the MRO, so while it would properly map
name=nombre on the first pass through the class it's trying to find, it
would then look at base classes, including the base Resource, and then
overwrite that mapping to go back to name=name. This was just seen as an
issue while applying the refactoring to AvailabilityZone in compute,
where the name=zoneName mapping wasn't properly working. This change
fixes that.

Change-Id: I21f343fdbf3c6d5696e3f074d56d96b4fe4faaba
This commit is contained in:
Brian Curtin
2016-04-05 10:31:23 -04:00
parent a784148b52
commit 605201f93b
2 changed files with 19 additions and 1 deletions

View File

@@ -332,7 +332,10 @@ class Resource(object):
for klass in cls.__mro__:
for key, value in klass.__dict__.items():
if isinstance(value, component):
mapping[key] = value.name
# Make sure base classes don't end up overwriting
# mappings we've found previously in subclasses.
if key not in mapping:
mapping[key] = value.name
return mapping
@classmethod

View File

@@ -509,6 +509,21 @@ class TestResource(base.TestCase):
self.assertIn("name", resource2.Resource._body_mapping())
self.assertIn("id", resource2.Resource._body_mapping())
def test__mapping_overrides(self):
# Iterating through the MRO used to wipe out overrides of mappings
# found in base classes.
new_name = "MyName"
new_id = "MyID"
class Test(resource2.Resource):
name = resource2.Body(new_name)
id = resource2.Body(new_id)
mapping = Test._body_mapping()
self.assertEqual(new_name, mapping["name"])
self.assertEqual(new_id, mapping["id"])
def test__body_mapping(self):
class Test(resource2.Resource):
x = resource2.Body("x")