Skip to content

Commit 8be17aa

Browse files
authored
Merge branch 'main' into dependabot/uv/python-packages-fa30b79da8
2 parents ddc3096 + 55265b5 commit 8be17aa

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

sqlmodel/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@ def model_dump(
903903
warnings: bool | Literal["none", "warn", "error"] = True,
904904
fallback: Callable[[Any], Any] | None = None, # v2.11
905905
serialize_as_any: bool = False, # v2.7
906+
polymorphic_serialization: bool | None = None, # v2.13
906907
) -> builtins.dict[str, Any]:
907908
if PYDANTIC_MINOR_VERSION < (2, 11):
908909
by_alias = by_alias or False
@@ -913,6 +914,8 @@ def model_dump(
913914
extra_kwargs["fallback"] = fallback
914915
if PYDANTIC_MINOR_VERSION >= (2, 12):
915916
extra_kwargs["exclude_computed_fields"] = exclude_computed_fields
917+
if PYDANTIC_MINOR_VERSION >= (2, 13):
918+
extra_kwargs["polymorphic_serialization"] = polymorphic_serialization
916919
return super().model_dump(
917920
mode=mode,
918921
include=include,
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import Any
2+
3+
import pytest
4+
from sqlmodel import SQLModel
5+
from sqlmodel._compat import PYDANTIC_MINOR_VERSION
6+
7+
8+
@pytest.mark.parametrize(
9+
("polymorphic_serialization", "expected_result"),
10+
[
11+
(None, {"user": {"name": "pydantic"}}),
12+
(False, {"user": {"name": "pydantic"}}),
13+
pytest.param(
14+
True,
15+
{"user": {"name": "pydantic", "password": "password"}},
16+
marks=pytest.mark.skipif(
17+
PYDANTIC_MINOR_VERSION < (2, 13),
18+
reason="polymorphic_serialization is only available in Pydantic v2.13+",
19+
),
20+
),
21+
],
22+
)
23+
def test_polymorphic_serialization(
24+
polymorphic_serialization: bool | None, expected_result: dict[str, Any]
25+
):
26+
27+
class User(SQLModel):
28+
name: str
29+
30+
class UserLogin(User):
31+
password: str
32+
33+
class OuterModel(SQLModel):
34+
user: User
35+
36+
outer_model = OuterModel(
37+
user=UserLogin(name="pydantic", password="password"),
38+
)
39+
40+
assert (
41+
outer_model.model_dump(polymorphic_serialization=polymorphic_serialization)
42+
== expected_result
43+
)
44+
45+
assert outer_model.model_dump(polymorphic_serialization=False) == {
46+
"user": {"name": "pydantic"}
47+
}

0 commit comments

Comments
 (0)