Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization transform stability improvements #336

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions dash_extensions/enrich.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,13 @@ class DataclassTransform(SerializationTransform):
def _try_load(self, data: Any, ann=None) -> Any:
if not dataclasses.is_dataclass(ann):
return data
return fromdict(ann, data)
if data is None:
return None
if isinstance(data, str):
data = json.loads(data)
if isinstance(data, dict):
return fromdict(ann, data)
raise ValueError(f"Unsupported data type for dataclass: {type(data)}")

def _try_dump(self, obj: Any) -> Any:
if not dataclasses.is_dataclass(obj):
Expand All @@ -1286,7 +1292,13 @@ class BaseModelTransform(SerializationTransform):
def _try_load(self, data: Any, ann=None) -> Any:
if not isinstance(ann, type(BaseModel)):
return data
return ann.model_validate_json(data)
if data is None:
return None
if isinstance(data, str):
return ann.model_validate_json(data)
if isinstance(data, dict):
return ann.model_validate(data)
raise ValueError(f"Unsupported data type for Pydantic model: {type(data)}")

def _try_dump(self, obj: Any) -> Any:
if not isinstance(obj, BaseModel):
Expand Down
Loading