Skip to content

Commit afbe459

Browse files
authored
Merge pull request #47 from ryantxu1/issue-46-custom-spec-valueerror
Adding logic to handle prop that end in "ref(s)"
2 parents 55d26bb + 4d0922d commit afbe459

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

stix2generator/stixcustom.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def _is_list_content_homogenous_type(list_):
3030
return result
3131

3232

33-
def _prop_for_spec(prop_spec, stix_version):
33+
def _prop_for_spec(prop_name, prop_spec, stix_version):
3434
"""
3535
Given an object generator spec, which is treated as a spec for a property
3636
value of an object, determine the type of value the spec generates and
@@ -45,8 +45,11 @@ def _prop_for_spec(prop_spec, stix_version):
4545
)
4646

4747
if prop_spec_type in _JSON_SIMPLE_TYPE_STIX_PROPERTY_MAP:
48-
prop_class = _JSON_SIMPLE_TYPE_STIX_PROPERTY_MAP[prop_spec_type]
49-
prop_obj = prop_class()
48+
if "ref" in prop_name or "refs" in prop_name:
49+
prop_obj = stix2.properties.ReferenceProperty(valid_types=prop_spec['stix-type'])
50+
else:
51+
prop_class = _JSON_SIMPLE_TYPE_STIX_PROPERTY_MAP[prop_spec_type]
52+
prop_obj = prop_class()
5053

5154
elif prop_spec_type == "object":
5255
# DictionaryProperty needs a spec_version parameter.
@@ -63,21 +66,23 @@ def _prop_for_spec(prop_spec, stix_version):
6366
raise stix2generator.exceptions.EmptyListError()
6467
if not _is_list_content_homogenous_type(prop_spec):
6568
raise stix2generator.exceptions.HeterogenousListError(prop_spec)
66-
6769
element_spec = prop_spec[0]
6870

6971
else:
7072
element_spec = prop_spec["items"]
7173

72-
element_prop_obj = _prop_for_spec(element_spec, stix_version)
73-
prop_obj = stix2.properties.ListProperty(element_prop_obj)
74+
element_prop_obj = _prop_for_spec("", element_spec, stix_version)
75+
76+
if "ref" in prop_name or "refs" in prop_name:
77+
prop_obj = stix2.properties.ListProperty(stix2.properties.ReferenceProperty(valid_types=element_spec["stix-type"], spec_version='2.1'))
78+
else:
79+
prop_obj = stix2.properties.ListProperty(element_prop_obj)
7480

7581
else:
7682
# Maybe we just hit this for "null" specs?
7783
raise stix2generator.exceptions.IllegalSTIXObjectPropertyType(
7884
prop_spec_type
7985
)
80-
8186
return prop_obj
8287

8388

@@ -110,7 +115,7 @@ def stix2_register_custom(spec, obj_type_name, stix_version):
110115
prop_specs = spec.get("properties", {})
111116

112117
prop_map = [
113-
(prop_name, _prop_for_spec(prop_spec, stix_version))
118+
(prop_name, _prop_for_spec(prop_name, prop_spec, stix_version))
114119
for prop_name, prop_spec in prop_specs.items()
115120
]
116121

0 commit comments

Comments
 (0)