Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization of reference fields in composite fields #83

Open
florimondmanca opened this issue Jul 9, 2019 · 1 comment
Open

Serialization of reference fields in composite fields #83

florimondmanca opened this issue Jul 9, 2019 · 1 comment

Comments

@florimondmanca
Copy link
Member

florimondmanca commented Jul 9, 2019

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 calling dict() on the parent class.

Consider the following schemas:

import typesystem as ts

class Child(ts.Schema):
    pass

class ParentA(ts.Schema):
    child = ts.Reference(to=Child)

class ParentB(ts.Schema):
    children = ts.Array(ts.Reference(Child))

class ParentC(ts.Schema):
    children = ts.Object(properties=ts.Reference(Child))

Test cases with what should happen IMO:

pa = ParentA(child=Child())
assert dict(pa) == {"child": {}}  # OK

pb = ParentB(children=[Child()])
assert dict(pb) == {"children": [{}]}  # FAIL
# {"children": [Child()]}

pc = ParentC(children=[Child()])
assert dict(pc) == {"children": {"john": {}}}  # FAIL
# {"children": {"john": Child()}}

This causes issues when passing the result of dict(pb) or dict(pc) to, say, json.dumps(), because it gets Child instances which are not JSON-serializable.

Am I simply abusing Array and Object? Is there another way of implementing composite fields with nested schemas?

@florimondmanca
Copy link
Member Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant