Skip to content

Commit 16f3b57

Browse files
author
Anthony Ruhier
committed
Fix recursive update with GET
Calling .get() recursively multiple times on the same object would break
1 parent b2efa78 commit 16f3b57

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

netboxapi/mapper.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,14 @@ def get(self, *args, limit=50, **kwargs):
5757
new_mappers_props = self._iterate_over_get_query(route, kwargs)
5858
for nm_prop in new_mappers_props:
5959
try:
60-
yield self._build_new_mapper_from(
61-
nm_prop, self._route + "{}/".format(nm_prop["id"])
62-
)
60+
if getattr(self, "id", None) is not None:
61+
new_mapper_route = route
62+
else:
63+
new_mapper_route = self._route + "{}/".format(
64+
nm_prop["id"]
65+
)
66+
67+
yield self._build_new_mapper_from(nm_prop, new_mapper_route)
6368
except (KeyError, TypeError):
6469
# Result objects have no id, cannot build a mapper from them,
6570
# yield them as received

tests/test_mapper.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ def test_get_id(self, mapper):
6767
assert obj.name == "test"
6868
assert obj.id == 1
6969

70+
def test_get_update_obj(self, mapper):
71+
url = self.get_mapper_url(mapper) + "1/"
72+
expected_attr = {"id": 1, "name": "test"}
73+
with requests_mock.Mocker() as m:
74+
m.register_uri("get", url, json=expected_attr)
75+
obj = next(mapper.get(1))
76+
obj_clone = next(obj.get())
77+
obj_clone = next(obj_clone.get())
78+
79+
assert obj.name == obj_clone.name
80+
assert obj.id == obj_clone.id
81+
7082
def test_get_query(self, mapper):
7183
url = self.get_mapper_url(mapper) + "?name=test"
7284
expected_attr = {

0 commit comments

Comments
 (0)