Skip to content

Commit 37fbc5e

Browse files
Serialize Base objects in MappedObject (#389)
1 parent bcb66cb commit 37fbc5e

3 files changed

Lines changed: 37 additions & 5 deletions

File tree

linode_api4/objects/base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,15 @@ def dict(self):
109109
result[k] = v.dict
110110
elif isinstance(v, list):
111111
result[k] = [
112-
item.dict if isinstance(item, cls) else item for item in v
112+
(
113+
item.dict
114+
if isinstance(item, cls)
115+
else item._raw_json if isinstance(item, Base) else item
116+
)
117+
for item in v
113118
]
114-
119+
elif isinstance(v, Base):
120+
result[k] = v._raw_json
115121
return result
116122

117123

test/fixtures/testmappedobj1.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"bar": "bar"
3+
}

test/unit/objects/mapped_object_test.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from unittest import TestCase
1+
from test.unit.base import ClientBaseCase
22

3-
from linode_api4 import MappedObject
3+
from linode_api4.objects import Base, MappedObject, Property
44

55

6-
class MappedObjectCase(TestCase):
6+
class MappedObjectCase(ClientBaseCase):
77
def test_mapped_object_dict(self):
88
test_dict = {
99
"key1": 1,
@@ -19,3 +19,26 @@ def test_mapped_object_dict(self):
1919

2020
mapped_obj = MappedObject(**test_dict)
2121
self.assertEqual(mapped_obj.dict, test_dict)
22+
23+
def test_mapped_object_dict(self):
24+
test_property_name = "bar"
25+
test_property_value = "bar"
26+
27+
class Foo(Base):
28+
api_endpoint = "/testmappedobj1"
29+
id_attribute = test_property_name
30+
properties = {
31+
test_property_name: Property(mutable=True),
32+
}
33+
34+
foo = Foo(self.client, test_property_value)
35+
foo._api_get()
36+
37+
expected_dict = {
38+
"foo": {
39+
test_property_name: test_property_value,
40+
}
41+
}
42+
43+
mapped_obj = MappedObject(foo=foo)
44+
self.assertEqual(mapped_obj.dict, expected_dict)

0 commit comments

Comments
 (0)