Skip to content

Commit dc0c37d

Browse files
🎨 Serialize Colors as HEX (#6839)
1 parent afd66d4 commit dc0c37d

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

packages/models-library/src/models_library/projects_nodes_ui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ class Position(BaseModel):
1616

1717

1818
class Marker(BaseModel):
19-
color: Annotated[Color, PlainSerializer(str), Field(...)]
19+
color: Annotated[Color, PlainSerializer(Color.as_hex), Field(...)]
2020

2121
model_config = ConfigDict(extra="forbid")

packages/models-library/src/models_library/projects_ui.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Slideshow(_SlideshowRequired, total=False):
3131

3232
class Annotation(BaseModel):
3333
type: Literal["note", "rect", "text"] = Field(...)
34-
color: Annotated[Color, PlainSerializer(str), Field(...)]
34+
color: Annotated[Color, PlainSerializer(Color.as_hex), Field(...)]
3535
attributes: dict = Field(..., description="svg attributes")
3636
model_config = ConfigDict(
3737
extra="forbid",
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import pytest
12
from models_library.projects_nodes_ui import Marker
23
from pydantic_extra_types.color import Color
34

45

5-
def test_marker_serialization():
6-
m = Marker(color=Color("#b7e28d"))
7-
8-
assert m.model_dump_json() == '{"color":"#b7e28d"}'
6+
@pytest.mark.parametrize(
7+
"color_str,expected_color_str", [("#b7e28d", "#b7e28d"), ("Cyan", "#0ff")]
8+
)
9+
def test_marker_color_serialized_to_hex(color_str, expected_color_str):
10+
m = Marker(color=Color(color_str))
11+
assert m.model_dump_json() == f'{{"color":"{expected_color_str}"}}'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
from models_library.projects_ui import Annotation
3+
from pydantic_extra_types.color import Color
4+
5+
6+
@pytest.mark.parametrize(
7+
"color_str,expected_color_str", [("#b7e28d", "#b7e28d"), ("Cyan", "#0ff")]
8+
)
9+
def test_annotation_color_serialized_to_hex(color_str, expected_color_str):
10+
m = Annotation(type="text", color=Color(color_str), attributes={})
11+
assert (
12+
m.model_dump_json()
13+
== f'{{"type":"text","color":"{expected_color_str}","attributes":{{}}}}'
14+
)

0 commit comments

Comments
 (0)