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

Optional List of dataclasses does not allow None Value #178

Open
JangasCodingplace opened this issue Jan 25, 2022 · 2 comments
Open

Optional List of dataclasses does not allow None Value #178

JangasCodingplace opened this issue Jan 25, 2022 · 2 comments

Comments

@JangasCodingplace
Copy link

Code

@dataclass
class AnyDc(JsonSchemaMixin):
    id: int


@dataclass
class Foo(JsonSchemaMixin):
    any_dc: Optional[List[AnyDc]] = None


if "__name__" == "__main__":
    d = {"any_dc": None}
    f = Foo.from_dict(d)

Error:

> dataclasses_jsonschema.ValidationError: None is not of type 'array'
@JangasCodingplace
Copy link
Author

Simpler Usecase:

@dataclass
class ReviewEntity(JsonSchemaMixin):
    source_type: str
    source: str
    review_id: str
    rating: int
    date: datetime.date
    text: str
    group: Optional[str] = None
    user: Optional[str] = None
    country: Optional[str] = None
    city: Optional[str] = None

Error Message for None at User field:

dataclasses_jsonschema.ValidationError: None is not of type 'string'

Failed validating 'type' in schema['properties']['user']:
    {'type': 'string'}

On instance['user']:
    None

@gdassori
Copy link

gdassori commented Mar 1, 2023

Hello, do you have any plans to fix this issue?

When using "omit_none=False" during serialization (which is available), it's impossible to deserialize back, so the flow is broken.

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

2 participants