diff --git a/packages/models-library/src/models_library/projects_nodes_ui.py b/packages/models-library/src/models_library/projects_nodes_ui.py index e14f2b21a28..d7d284e3afb 100644 --- a/packages/models-library/src/models_library/projects_nodes_ui.py +++ b/packages/models-library/src/models_library/projects_nodes_ui.py @@ -2,7 +2,9 @@ Models node UI (legacy model, use instead projects.ui.py) """ -from pydantic import BaseModel, ConfigDict, Field +from typing import Annotated + +from pydantic import BaseModel, ConfigDict, Field, PlainSerializer from pydantic_extra_types.color import Color @@ -14,6 +16,6 @@ class Position(BaseModel): class Marker(BaseModel): - color: Color = Field(...) + color: Annotated[Color, PlainSerializer(str), Field(...)] model_config = ConfigDict(extra="forbid") diff --git a/packages/models-library/src/models_library/projects_ui.py b/packages/models-library/src/models_library/projects_ui.py index 64556b8c044..a7f6dbc7531 100644 --- a/packages/models-library/src/models_library/projects_ui.py +++ b/packages/models-library/src/models_library/projects_ui.py @@ -2,9 +2,9 @@ Models Front-end UI """ -from typing import Literal +from typing import Annotated, Literal -from pydantic import BaseModel, ConfigDict, Field, field_validator +from pydantic import BaseModel, ConfigDict, Field, PlainSerializer, field_validator from pydantic_extra_types.color import Color from typing_extensions import ( # https://docs.pydantic.dev/latest/api/standard_library_types/#typeddict TypedDict, @@ -31,7 +31,7 @@ class Slideshow(_SlideshowRequired, total=False): class Annotation(BaseModel): type: Literal["note", "rect", "text"] = Field(...) - color: Color = Field(...) + color: Annotated[Color, PlainSerializer(str), Field(...)] attributes: dict = Field(..., description="svg attributes") model_config = ConfigDict( extra="forbid", diff --git a/packages/models-library/tests/test_projects_nodes_ui.py b/packages/models-library/tests/test_projects_nodes_ui.py new file mode 100644 index 00000000000..3ec33ca677d --- /dev/null +++ b/packages/models-library/tests/test_projects_nodes_ui.py @@ -0,0 +1,8 @@ +from models_library.projects_nodes_ui import Marker +from pydantic_extra_types.color import Color + + +def test_marker_serialization(): + m = Marker(color=Color("#b7e28d")) + + assert m.model_dump_json() == '{"color":"#b7e28d"}'