Skip to content

Adding support for dataclass Union field #63

@panchock

Description

@panchock

Hi there!

We are starting using this package a lot in our product and it's great!
One gap we reached into is support for dataclass union (typing.Union[dataclass1, dataclass2, ...]) as one of the dataclass fields.

Example case:

@dataclass
class A:
    a: int


@dataclass
class B:
    b: int


@dataclass
class Response:
    obj: A | B

class ResponseSerializer(DataclassSerializer)
    class Meta:
        dataclass = Response

I solved this by extending DataclassSerializer and add support using rest-polymorphic.
We also use drf-spectacular for openapi scheme and it also supports rest-polymorphic, so all good!

Before I open PR I wanted to check that this feature is useful, I would be happy to get a feedback :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions