Pass allow_custom to object dicts in a Bundle
parent
69c31ca3fc
commit
cf972479ed
|
@ -136,7 +136,7 @@ class _STIXBase(collections.Mapping):
|
||||||
if custom_props and not isinstance(custom_props, dict):
|
if custom_props and not isinstance(custom_props, dict):
|
||||||
raise ValueError("'custom_properties' must be a dictionary")
|
raise ValueError("'custom_properties' must be a dictionary")
|
||||||
if not allow_custom:
|
if not allow_custom:
|
||||||
extra_kwargs = list(set(kwargs) - set(cls._properties))
|
extra_kwargs = list(set(kwargs) - set(self._properties))
|
||||||
if extra_kwargs:
|
if extra_kwargs:
|
||||||
raise ExtraPropertiesError(cls, extra_kwargs)
|
raise ExtraPropertiesError(cls, extra_kwargs)
|
||||||
|
|
||||||
|
@ -149,17 +149,17 @@ class _STIXBase(collections.Mapping):
|
||||||
setting_kwargs[prop_name] = prop_value
|
setting_kwargs[prop_name] = prop_value
|
||||||
|
|
||||||
# Detect any missing required properties
|
# Detect any missing required properties
|
||||||
required_properties = set(get_required_properties(cls._properties))
|
required_properties = set(get_required_properties(self._properties))
|
||||||
missing_kwargs = required_properties - set(setting_kwargs)
|
missing_kwargs = required_properties - set(setting_kwargs)
|
||||||
if missing_kwargs:
|
if missing_kwargs:
|
||||||
raise MissingPropertiesError(cls, missing_kwargs)
|
raise MissingPropertiesError(cls, missing_kwargs)
|
||||||
|
|
||||||
for prop_name, prop_metadata in cls._properties.items():
|
for prop_name, prop_metadata in self._properties.items():
|
||||||
self._check_property(prop_name, prop_metadata, setting_kwargs)
|
self._check_property(prop_name, prop_metadata, setting_kwargs)
|
||||||
|
|
||||||
# Cache defaulted optional properties for serialization
|
# Cache defaulted optional properties for serialization
|
||||||
defaulted = []
|
defaulted = []
|
||||||
for name, prop in cls._properties.items():
|
for name, prop in self._properties.items():
|
||||||
try:
|
try:
|
||||||
if (not prop.required and not hasattr(prop, '_fixed_value') and
|
if (not prop.required and not hasattr(prop, '_fixed_value') and
|
||||||
prop.default() == setting_kwargs[name]):
|
prop.default() == setting_kwargs[name]):
|
||||||
|
|
|
@ -63,7 +63,7 @@ class Bundle(_STIXBase):
|
||||||
kwargs['objects'] = list(args) + kwargs.get('objects', [])
|
kwargs['objects'] = list(args) + kwargs.get('objects', [])
|
||||||
|
|
||||||
self.__allow_custom = kwargs.get('allow_custom', False)
|
self.__allow_custom = kwargs.get('allow_custom', False)
|
||||||
self._properties['objects'].allow_custom = kwargs.get('allow_custom', False)
|
self._properties['objects'].contained.allow_custom = kwargs.get('allow_custom', False)
|
||||||
|
|
||||||
super(Bundle, self).__init__(**kwargs)
|
super(Bundle, self).__init__(**kwargs)
|
||||||
|
|
||||||
|
|
|
@ -88,13 +88,31 @@ def test_parse_identity_custom_property(data):
|
||||||
assert identity.foo == "bar"
|
assert identity.foo == "bar"
|
||||||
|
|
||||||
|
|
||||||
def test_custom_property_in_bundled_object():
|
def test_custom_property_object_in_bundled_object():
|
||||||
bundle = stix2.Bundle(IDENTITY_CUSTOM_PROP, allow_custom=True)
|
bundle = stix2.Bundle(IDENTITY_CUSTOM_PROP, allow_custom=True)
|
||||||
|
|
||||||
assert bundle.objects[0].x_foo == "bar"
|
assert bundle.objects[0].x_foo == "bar"
|
||||||
assert '"x_foo": "bar"' in str(bundle)
|
assert '"x_foo": "bar"' in str(bundle)
|
||||||
|
|
||||||
|
|
||||||
|
def test_custom_property_dict_in_bundled_object():
|
||||||
|
custom_identity = {
|
||||||
|
'type': 'identity',
|
||||||
|
'id': 'identity--311b2d2d-f010-5473-83ec-1edf84858f4c',
|
||||||
|
'created': '2015-12-21T19:59:11Z',
|
||||||
|
'name': 'John Smith',
|
||||||
|
'identity_class': 'individual',
|
||||||
|
'x_foo': 'bar',
|
||||||
|
}
|
||||||
|
with pytest.raises(stix2.exceptions.ExtraPropertiesError):
|
||||||
|
bundle = stix2.Bundle(custom_identity)
|
||||||
|
|
||||||
|
bundle = stix2.Bundle(custom_identity, allow_custom=True)
|
||||||
|
|
||||||
|
assert bundle.objects[0].x_foo == "bar"
|
||||||
|
assert '"x_foo": "bar"' in str(bundle)
|
||||||
|
|
||||||
|
|
||||||
def test_custom_property_in_observed_data():
|
def test_custom_property_in_observed_data():
|
||||||
artifact = stix2.File(
|
artifact = stix2.File(
|
||||||
allow_custom=True,
|
allow_custom=True,
|
||||||
|
|
Loading…
Reference in New Issue