s-knibbs / dataclasses-jsonschema

JSON schema generation from dataclasses
MIT License
166 stars 38 forks source link

Support discriminators when used with Union types #170

Closed s-knibbs closed 2 years ago

s-knibbs commented 2 years ago

It should be possible to use discriminators when decoding union fields. Currently discriminators only work when calling from_dict on the base class.

Example:

@dataclass
class Base(JsonSchemaMixin, discriminator=True):
    pass

@dataclass
class Foo(Base):
    common_field: int

@dataclass
class Bar(Base):
    common_field: int
    other_field: Optional[int] = None

@dataclass
class Baz(JsonSchemaMixin):
    my_foo_bar: Union[Bar, Foo]

This doesn't currently work since, since data can be valid under both the Foo and Bar schemas and the discriminator field is not checked.