From 07f315903b5f25643d6827c6e8df393423f60a68 Mon Sep 17 00:00:00 2001 From: Giancarlo Romeo Date: Tue, 26 Nov 2024 11:49:51 +0100 Subject: [PATCH] Serialize Colors as HEX --- .../src/models_library/projects_nodes_ui.py | 2 +- .../src/models_library/projects_ui.py | 2 +- .../models-library/tests/test_projects_nodes_ui.py | 11 +++++++---- packages/models-library/tests/test_projects_ui.py | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 packages/models-library/tests/test_projects_ui.py 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 d7d284e3afb..c5990598104 100644 --- a/packages/models-library/src/models_library/projects_nodes_ui.py +++ b/packages/models-library/src/models_library/projects_nodes_ui.py @@ -16,6 +16,6 @@ class Position(BaseModel): class Marker(BaseModel): - color: Annotated[Color, PlainSerializer(str), Field(...)] + color: Annotated[Color, PlainSerializer(Color.as_hex), 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 a7f6dbc7531..170f3becc64 100644 --- a/packages/models-library/src/models_library/projects_ui.py +++ b/packages/models-library/src/models_library/projects_ui.py @@ -31,7 +31,7 @@ class Slideshow(_SlideshowRequired, total=False): class Annotation(BaseModel): type: Literal["note", "rect", "text"] = Field(...) - color: Annotated[Color, PlainSerializer(str), Field(...)] + color: Annotated[Color, PlainSerializer(Color.as_hex), 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 index 3ec33ca677d..54a91c82832 100644 --- a/packages/models-library/tests/test_projects_nodes_ui.py +++ b/packages/models-library/tests/test_projects_nodes_ui.py @@ -1,8 +1,11 @@ +import pytest 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"}' +@pytest.mark.parametrize( + "color_str,expected_color_str", [("#b7e28d", "#b7e28d"), ("Cyan", "#0ff")] +) +def test_marker_color_serialized_to_hex(color_str, expected_color_str): + m = Marker(color=Color(color_str)) + assert m.model_dump_json() == f'{{"color":"{expected_color_str}"}}' diff --git a/packages/models-library/tests/test_projects_ui.py b/packages/models-library/tests/test_projects_ui.py new file mode 100644 index 00000000000..9cfdac1980c --- /dev/null +++ b/packages/models-library/tests/test_projects_ui.py @@ -0,0 +1,14 @@ +import pytest +from models_library.projects_ui import Annotation +from pydantic_extra_types.color import Color + + +@pytest.mark.parametrize( + "color_str,expected_color_str", [("#b7e28d", "#b7e28d"), ("Cyan", "#0ff")] +) +def test_annotation_color_serialized_to_hex(color_str, expected_color_str): + m = Annotation(type="text", color=Color(color_str), attributes={}) + assert ( + m.model_dump_json() + == f'{{"type":"text","color":"{expected_color_str}","attributes":{{}}}}' + )