From 4bdbb4a2f967405ae752aeac33cf480318f9b06e Mon Sep 17 00:00:00 2001 From: clenk Date: Tue, 18 Jul 2017 12:05:19 -0400 Subject: [PATCH] Deep-copy in Object Factory and allow appending multiple items to list properties in ObjectFactory.create() --- stix2/environment.py | 10 ++++++++-- stix2/test/test_environment.py | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/stix2/environment.py b/stix2/environment.py index a3d79bc..f855755 100644 --- a/stix2/environment.py +++ b/stix2/environment.py @@ -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) diff --git a/stix2/test/test_environment.py b/stix2/test/test_environment.py index 9e5ab98..9be8101 100644 --- a/stix2/test/test_environment.py +++ b/stix2/test/test_environment.py @@ -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",