I'm having trouble with Unions of classes.
If you have subclasses of data classes the serialisation and deserialization behaviour depends on the order of the Union. However, it is not symmetrical if you also have default values in your data classes ie if the union is one way around serialisation succeeds but then deserialisation fails and vice versa.
For example:
@dataclass
class Base:
name: str
value: int = None
@dataclass
class SubBase(Base):
value2: int = None
@dataclass
class Container:
base: Union[Base, SubBase]
c = Container(SubBase("Test", 1, 2))
schema: Schema = marshmallow_dataclass.class_schema(Container)()
print(schema.dumps(c))
which incorrectly prints the following:
{"base": {"name": "Test", "value": 1}}
It recognises that SubBase is an instance of Base so uses the the Base serialiser and only serializes the fields in Base.
I'm having trouble with Unions of classes. If you have subclasses of data classes the serialisation and deserialization behaviour depends on the order of the Union. However, it is not symmetrical if you also have default values in your data classes ie if the union is one way around serialisation succeeds but then deserialisation fails and vice versa.
For example:
which incorrectly prints the following:
It recognises that SubBase is an instance of Base so uses the the Base serialiser and only serializes the fields in Base.
For deserialization:
you get the right values:
However, if you switch the Union around you get the correct serialisation:
but now the deserialization is wrong:
This is because it's using the default value for value2 in SubBase to create a valid SubBase instance using only the arguments for Base.
I'm not sure if there is any way around this.