Skip to content

Commit

Permalink
🐛 Fix Color serialization in Pydantic v2 (#6821)
Browse files Browse the repository at this point in the history
  • Loading branch information
giancarloromeo authored Nov 25, 2024
1 parent 532c977 commit 9870a85
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -14,6 +16,6 @@ class Position(BaseModel):


class Marker(BaseModel):
color: Color = Field(...)
color: Annotated[Color, PlainSerializer(str), Field(...)]

model_config = ConfigDict(extra="forbid")
6 changes: 3 additions & 3 deletions packages/models-library/src/models_library/projects_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions packages/models-library/tests/test_projects_nodes_ui.py
Original file line number Diff line number Diff line change
@@ -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"}'

0 comments on commit 9870a85

Please sign in to comment.