Skip to content

Commit 5e2c9bc

Browse files
cccs-mdrCopilotcccs-hxp
authored
fix(api): deprecate related.id in favour of related.ids (#486)
* fix(api): deprecate related.id in favour of related.ids * one other test * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Update api/howler/odm/models/ecs/related.py Co-authored-by: cccs-hxp <xuan.pham@cyber.gc.ca> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: cccs-hxp <xuan.pham@cyber.gc.ca>
1 parent d5c2167 commit 5e2c9bc

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

api/howler/odm/models/ecs/related.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ class Related(odm.Model):
3333
)
3434

3535
# Extra fields not defined in ECS but added for outline purposes
36-
id = odm.Optional(odm.Keyword(description="The id related to the event."))
36+
id = odm.Optional(
37+
odm.Keyword(
38+
description="The id related to the event.",
39+
deprecated=True,
40+
deprecated_description="related.ids should be used instead of related.id.",
41+
)
42+
)
3743

3844
uri = odm.Optional(odm.List(odm.URI(), description="All of the URIs related to the event."))
3945

@@ -43,3 +49,23 @@ class Related(odm.Model):
4349
description="All the signatures/rules that were triggered by the event.",
4450
)
4551
)
52+
53+
def __init__(self, data: dict = None, *args, **kwargs):
54+
if data is not None and data.get("id") is not None:
55+
# Avoid mutating the caller-provided dict
56+
data = dict(data)
57+
58+
existing_ids = data.get("ids")
59+
if isinstance(existing_ids, (list, tuple)):
60+
merged_ids = list(existing_ids)
61+
elif existing_ids is None:
62+
merged_ids = []
63+
else:
64+
merged_ids = [existing_ids]
65+
66+
if data["id"] not in merged_ids:
67+
merged_ids.append(data["id"])
68+
69+
data["ids"] = merged_ids
70+
71+
super().__init__(data, *args, **kwargs)

api/test/unit/odm/test_related.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from howler.odm.models.ecs.related import Related
2+
from howler.odm.models.hit import Hit
3+
4+
5+
class TestRelated:
6+
def test_related_id_is_copied_to_ids(self):
7+
related = Related({"id": "indicator-1"})
8+
9+
assert related.id == "indicator-1"
10+
assert related.ids == ["indicator-1"]
11+
12+
def test_related_id_merges_into_existing_ids_without_duplicates(self):
13+
related = Related({"id": "indicator-2", "ids": ["indicator-1", "indicator-2"]})
14+
15+
assert related.id == "indicator-2"
16+
assert related.ids == ["indicator-1", "indicator-2"]
17+
18+
def test_related_id_merges_into_existing_ids_tuple(self):
19+
related = Related({"id": "indicator-2", "ids": ("indicator-1", "indicator-2")})
20+
21+
assert related.id == "indicator-2"
22+
assert related.ids == ["indicator-1", "indicator-2"]
23+
24+
def test_parent_odm_related_id_key_is_copied_to_related_ids(self):
25+
hit = Hit({"howler.analytic": "Test Analytic", "howler.hash": "a", "related.id": "indicator-3"})
26+
27+
assert hit.related is not None
28+
assert hit.related.id == "indicator-3"
29+
assert hit.related.ids == ["indicator-3"]

0 commit comments

Comments
 (0)