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

Pipe | as alternative of Union is not working properly #195

@visko-sc

Description

@visko-sc
from dataclasses import dataclass

from dataclasses_jsonschema import JsonSchemaMixin


@dataclass
class Numbers(JsonSchemaMixin):
    roman: str


@dataclass
class Math(JsonSchemaMixin):
    numbers: Numbers | None = None


print(Math(Numbers('X')).to_dict())

Actual result:
{'numbers': Numbers(roman='X')}

Expected result:
{'numbers': {'roman': 'X'}}

I think the problem can be fixed in either _get_field_type_name or in

elif field_type_name == "Union":

Note:
These work as expected:

@dataclass
class Math2(JsonSchemaMixin):
    numbers: Union[Numbers, None] = None


@dataclass
class Math3(JsonSchemaMixin):
    numbers: Optional[Numbers] = None

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