@@ -114,6 +114,9 @@ def _flatten_base_subclass(obj: "Base") -> Optional[Dict[str, Any]]:
114
114
115
115
@property
116
116
def dict (self ):
117
+ return self ._serialize ()
118
+
119
+ def _serialize (self , is_put : bool = False ) -> Dict [str , Any ]:
117
120
result = vars (self ).copy ()
118
121
cls = type (self )
119
122
@@ -123,7 +126,7 @@ def dict(self):
123
126
elif isinstance (v , list ):
124
127
result [k ] = [
125
128
(
126
- item .dict
129
+ item ._serialize ( is_put = is_put )
127
130
if isinstance (item , (cls , JSONObject ))
128
131
else (
129
132
self ._flatten_base_subclass (item )
@@ -136,7 +139,7 @@ def dict(self):
136
139
elif isinstance (v , Base ):
137
140
result [k ] = self ._flatten_base_subclass (v )
138
141
elif isinstance (v , JSONObject ):
139
- result [k ] = v .dict
142
+ result [k ] = v ._serialize ( is_put = is_put )
140
143
141
144
return result
142
145
@@ -278,9 +281,9 @@ def save(self, force=True) -> bool:
278
281
data [key ] = None
279
282
280
283
# Ensure we serialize any values that may not be already serialized
281
- data = _flatten_request_body_recursive (data )
284
+ data = _flatten_request_body_recursive (data , is_put = True )
282
285
else :
283
- data = self ._serialize ()
286
+ data = self ._serialize (is_put = True )
284
287
285
288
resp = self ._client .put (type (self ).api_endpoint , model = self , data = data )
286
289
@@ -316,7 +319,7 @@ def invalidate(self):
316
319
317
320
self ._set ("_populated" , False )
318
321
319
- def _serialize (self ):
322
+ def _serialize (self , is_put : bool = False ):
320
323
"""
321
324
A helper method to build a dict of all mutable Properties of
322
325
this object
@@ -345,7 +348,7 @@ def _serialize(self):
345
348
346
349
# Resolve the underlying IDs of results
347
350
for k , v in result .items ():
348
- result [k ] = _flatten_request_body_recursive (v )
351
+ result [k ] = _flatten_request_body_recursive (v , is_put = is_put )
349
352
350
353
return result
351
354
@@ -503,7 +506,7 @@ def make_instance(cls, id, client, parent_id=None, json=None):
503
506
return Base .make (id , client , cls , parent_id = parent_id , json = json )
504
507
505
508
506
- def _flatten_request_body_recursive (data : Any ) -> Any :
509
+ def _flatten_request_body_recursive (data : Any , is_put : bool = False ) -> Any :
507
510
"""
508
511
This is a helper recursively flatten the given data for use in an API request body.
509
512
@@ -515,15 +518,18 @@ def _flatten_request_body_recursive(data: Any) -> Any:
515
518
"""
516
519
517
520
if isinstance (data , dict ):
518
- return {k : _flatten_request_body_recursive (v ) for k , v in data .items ()}
521
+ return {
522
+ k : _flatten_request_body_recursive (v , is_put = is_put )
523
+ for k , v in data .items ()
524
+ }
519
525
520
526
if isinstance (data , list ):
521
- return [_flatten_request_body_recursive (v ) for v in data ]
527
+ return [_flatten_request_body_recursive (v , is_put = is_put ) for v in data ]
522
528
523
529
if isinstance (data , Base ):
524
530
return data .id
525
531
526
532
if isinstance (data , MappedObject ) or issubclass (type (data ), JSONObject ):
527
- return data .dict
533
+ return data ._serialize ( is_put = is_put )
528
534
529
535
return data
0 commit comments