Skip to content

🎨 Serialize Colors as HEX #6839

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

Merged
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 7 additions & 4 deletions packages/models-library/tests/test_projects_nodes_ui.py
Original file line number Diff line number Diff line change
@@ -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}"}}'
14 changes: 14 additions & 0 deletions packages/models-library/tests/test_projects_ui.py
Original file line number Diff line number Diff line change
@@ -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":{{}}}}'
)
Loading