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

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

Open
visko-sc opened this issue Oct 11, 2022 · 3 comments
Open

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

visko-sc opened this issue Oct 11, 2022 · 3 comments

Comments

@visko-sc
Copy link

visko-sc commented Oct 11, 2022

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
@ZdenekM
Copy link

ZdenekM commented Oct 27, 2022

Duplicate of #187?

@visko-sc
Copy link
Author

They are related. But while #187 brings a specific problem (str | None) and schema creation, this is about the pipe operator and data class creation error. The root cause is probably the same.

@s-knibbs
Copy link
Owner

s-knibbs commented May 4, 2023

Adding from __future__ import annotations at the top fixed the example code you posted.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants