Deep-copy in Object Factory and allow appending multiple items to list

properties in ObjectFactory.create()
stix2.1
clenk 2017-07-18 12:05:19 -04:00
parent 0a2cda00cc
commit 4bdbb4a2f9
2 changed files with 13 additions and 2 deletions

View File

@ -1,3 +1,5 @@
import copy
class ObjectFactory(object):
"""Object Factory
@ -44,7 +46,7 @@ class ObjectFactory(object):
def create(self, cls, **kwargs):
# Use self.defaults as the base, but update with any explicit args
# provided by the user.
properties = dict(**self._defaults)
properties = copy.deepcopy(self._defaults)
if kwargs:
if self._list_append:
# Append provided items to list properties instead of replacing them
@ -55,7 +57,11 @@ class ObjectFactory(object):
continue
if not isinstance(properties[list_prop], list):
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)

View File

@ -62,10 +62,15 @@ def test_object_factory_list_append():
description="Threat report from ACME")
ext_ref2 = stix2.ExternalReference(source_name="Yet Another Threat Report",
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)
ind = factory.create(stix2.Indicator, external_references=ext_ref2, **INDICATOR_KWARGS)
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():
ext_ref = stix2.ExternalReference(source_name="ACME Threat Intel",