Deep-copy in Object Factory and allow appending multiple items to list
properties in ObjectFactory.create()stix2.1
parent
0a2cda00cc
commit
4bdbb4a2f9
|
@ -1,3 +1,5 @@
|
||||||
|
import copy
|
||||||
|
|
||||||
|
|
||||||
class ObjectFactory(object):
|
class ObjectFactory(object):
|
||||||
"""Object Factory
|
"""Object Factory
|
||||||
|
@ -44,7 +46,7 @@ class ObjectFactory(object):
|
||||||
def create(self, cls, **kwargs):
|
def create(self, cls, **kwargs):
|
||||||
# Use self.defaults as the base, but update with any explicit args
|
# Use self.defaults as the base, but update with any explicit args
|
||||||
# provided by the user.
|
# provided by the user.
|
||||||
properties = dict(**self._defaults)
|
properties = copy.deepcopy(self._defaults)
|
||||||
if kwargs:
|
if kwargs:
|
||||||
if self._list_append:
|
if self._list_append:
|
||||||
# Append provided items to list properties instead of replacing them
|
# Append provided items to list properties instead of replacing them
|
||||||
|
@ -55,7 +57,11 @@ class ObjectFactory(object):
|
||||||
continue
|
continue
|
||||||
if not isinstance(properties[list_prop], list):
|
if not isinstance(properties[list_prop], list):
|
||||||
properties[list_prop] = [properties[list_prop]]
|
properties[list_prop] = [properties[list_prop]]
|
||||||
properties[list_prop].append(kwarg_prop)
|
|
||||||
|
if isinstance(kwarg_prop, list):
|
||||||
|
properties[list_prop].extend(kwarg_prop)
|
||||||
|
else:
|
||||||
|
properties[list_prop].append(kwarg_prop)
|
||||||
|
|
||||||
properties.update(**kwargs)
|
properties.update(**kwargs)
|
||||||
|
|
||||||
|
|
|
@ -62,10 +62,15 @@ def test_object_factory_list_append():
|
||||||
description="Threat report from ACME")
|
description="Threat report from ACME")
|
||||||
ext_ref2 = stix2.ExternalReference(source_name="Yet Another Threat Report",
|
ext_ref2 = stix2.ExternalReference(source_name="Yet Another Threat Report",
|
||||||
description="Threat report from YATR")
|
description="Threat report from YATR")
|
||||||
|
ext_ref3 = stix2.ExternalReference(source_name="Threat Report #3",
|
||||||
|
description="One more threat report")
|
||||||
factory = stix2.ObjectFactory(external_references=ext_ref)
|
factory = stix2.ObjectFactory(external_references=ext_ref)
|
||||||
ind = factory.create(stix2.Indicator, external_references=ext_ref2, **INDICATOR_KWARGS)
|
ind = factory.create(stix2.Indicator, external_references=ext_ref2, **INDICATOR_KWARGS)
|
||||||
assert ind.external_references[1].source_name == "Yet Another Threat Report"
|
assert ind.external_references[1].source_name == "Yet Another Threat Report"
|
||||||
|
|
||||||
|
ind = factory.create(stix2.Indicator, external_references=[ext_ref2, ext_ref3], **INDICATOR_KWARGS)
|
||||||
|
assert ind.external_references[2].source_name == "Threat Report #3"
|
||||||
|
|
||||||
|
|
||||||
def test_object_factory_list_replace():
|
def test_object_factory_list_replace():
|
||||||
ext_ref = stix2.ExternalReference(source_name="ACME Threat Intel",
|
ext_ref = stix2.ExternalReference(source_name="ACME Threat Intel",
|
||||||
|
|
Loading…
Reference in New Issue