Open florimondmanca opened 5 years ago
FWIW, I implemented a workaround in the form of this helper:
def _serialize_nested(value: typing.Any) -> dict:
if isinstance(value, ts.Schema):
return _serialize_nested(dict(value))
if isinstance(value, list):
return [_serialize_nested(item) for item in value]
if isinstance(value, dict):
return {key: _serialize_nested(val) for key, val in value.items()}
return value
Usage:
pb = ParentB(children=[Child()])
assert _serialize_nested(pb) == {"children": [{}]} # OK
Hey,
I was fiddling with composite data types, in particular with
Reference
to implement nested schemas, and there seems to be an issue with how arrays and objects of references are serialized when callingdict()
on the parent class.Consider the following schemas:
Test cases with what should happen IMO:
This causes issues when passing the result of
dict(pb)
ordict(pc)
to, say,json.dumps()
, because it getsChild
instances which are not JSON-serializable.Am I simply abusing
Array
andObject
? Is there another way of implementing composite fields with nested schemas?