Skip to content

Commit

Permalink
Fix user-defined model serialization
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Hayes <[email protected]>
  • Loading branch information
JacobHayes committed Oct 11, 2023
1 parent d155ab0 commit 08bcfec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/hera/shared/_pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
__all__ = [
"BaseModel",
"Field",
"PydanticBaseModel", # Export for serialization.py to cover user-defined models
"root_validator",
"validate_arguments",
"validator",
Expand Down
12 changes: 8 additions & 4 deletions src/hera/shared/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
from json import JSONEncoder
from typing import Any, Optional

from hera.shared._pydantic import BaseModel
# NOTE: Use the original BaseModel in order to support serializing user-defined models,
# which won't use our hera.shared._pydantic import. This does still require that the
# user-defined models are using v1 pydantic models for now (either from a pydantic v1
# installation or `pydantic.v1` import from a pydantic v2 installation).
from hera.shared._pydantic import PydanticBaseModel

MISSING = object()
"""`MISSING` is a placeholder that indicates field value nullity.
"""`MISSING` is a placeholder that indicates field value nullity.
When the user of a Hera object sets the field of an object specifically to `None`, Hera needs to distinguish between
default nullity/None and user-provided `None` on, say, something like the `source` of `Script`.
default nullity/None and user-provided `None` on, say, something like the `source` of `Script`.
"""


Expand All @@ -18,7 +22,7 @@ class PydanticEncoder(JSONEncoder):

def default(self, o: Any):
"""Return the default representation of the given object."""
if isinstance(o, BaseModel):
if isinstance(o, PydanticBaseModel):
return o.dict(by_alias=True)
return super().default(o)

Expand Down

0 comments on commit 08bcfec

Please sign in to comment.