Raise exception when synthetic field invalid
A check is introduced in the ``__init__`` method of ``NeutronObject`` class. This check ensures synthetic fields are valid in any oslo versioned object. Reference: I3c565f83098a03d210616f51d359ca45388aaf60 Depends-On: I12c7a330555966d30e44418cbd500958fe844462 Co-Authored-By: Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com> Closes-Bug: #1614478 Change-Id: I33c41fbd4dd40ba292ebe67ac497372d4354f260
This commit is contained in:
parent
b520b16d8a
commit
7ccb2994b8
|
@ -290,6 +290,11 @@ class DeclarativeObject(abc.ABCMeta):
|
|||
if 'project_id' in cls.fields and 'tenant_id' not in cls.fields:
|
||||
cls.extra_filter_names.add('tenant_id')
|
||||
|
||||
invalid_fields = [f for f in cls.synthetic_fields
|
||||
if f not in cls.fields]
|
||||
if invalid_fields:
|
||||
raise o_exc.NeutronObjectValidatorException(fields=invalid_fields)
|
||||
|
||||
|
||||
@six.add_metaclass(DeclarativeObject)
|
||||
class NeutronDbObject(NeutronObject):
|
||||
|
|
|
@ -1991,3 +1991,24 @@ class OperationOnStringAndJsonTestCase(test_base.BaseTestCase):
|
|||
res = base.NeutronDbObject.filter_to_json_str(field_val,
|
||||
default_val)
|
||||
self.assertEqual(default_val, res)
|
||||
|
||||
|
||||
class NeutronObjectValidatorTestCase(test_base.BaseTestCase):
|
||||
|
||||
def test_load_wrong_synthetic_fields(self):
|
||||
try:
|
||||
@obj_base.VersionedObjectRegistry.register_if(False)
|
||||
class FakeNeutronObjectSyntheticFieldWrong(base.NeutronDbObject):
|
||||
# Version 1.0: Initial version
|
||||
VERSION = '1.0'
|
||||
|
||||
db_model = FakeModel
|
||||
|
||||
fields = {
|
||||
'id': common_types.UUIDField(),
|
||||
'obj_field': common_types.UUIDField()
|
||||
}
|
||||
|
||||
synthetic_fields = ['obj_field', 'wrong_synthetic_field_name']
|
||||
except o_exc.NeutronObjectValidatorException as exc:
|
||||
self.assertIn('wrong_synthetic_field_name', str(exc))
|
||||
|
|
Loading…
Reference in New Issue