Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Serialization of reference fields in composite fields #83

@florimondmanca

Description

@florimondmanca

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions