11import pytest
22from sqlmodel import Field , SQLModel
3+ from sqlmodel ._compat import IS_PYDANTIC_V2
4+
5+ from tests .conftest import needs_pydanticv2
36
47
58def test_json_schema_extra_applied ():
@@ -13,15 +16,18 @@ class Item(SQLModel):
1316 }
1417 )
1518
16- schema = Item .model_json_schema ()
19+ if IS_PYDANTIC_V2 :
20+ schema = Item .model_json_schema ()
21+ else :
22+ schema = Item .schema ()
1723
1824 name_schema = schema ["properties" ]["name" ]
1925
2026 assert name_schema ["example" ] == "Sword of Power"
2127 assert name_schema ["x-custom-key" ] == "Important Data"
2228
2329
24- def test_schema_extra_and_new_param_conflict (caplog ):
30+ def test_schema_extra_and_json_schema_extra_conflict (caplog ):
2531 """
2632 Test that passing schema_extra and json_schema_extra at the same time produces
2733 a warning.
@@ -47,19 +53,29 @@ class LegacyItem(SQLModel):
4753 }
4854 )
4955
50- schema = LegacyItem .model_json_schema ()
56+ if IS_PYDANTIC_V2 :
57+ schema = LegacyItem .model_json_schema ()
58+ else :
59+ schema = LegacyItem .schema ()
5160
5261 name_schema = schema ["properties" ]["name" ]
5362
5463 assert name_schema ["example" ] == "Sword of Old"
5564 assert name_schema ["x-custom-key" ] == "Important Data"
5665
57- field_info = LegacyItem .model_fields ["name" ]
58- assert field_info .serialization_alias == "id_test"
66+ if IS_PYDANTIC_V2 :
67+ # With Pydantic V1 serialization_alias from schema_extra is applied
68+ field_info = LegacyItem .model_fields ["name" ]
69+ assert field_info .serialization_alias == "id_test"
70+ else : # With Pydantic V1 it just goes to schema
71+ assert name_schema ["serialization_alias" ] == "id_test"
5972
6073
74+ @needs_pydanticv2
6175def test_json_schema_extra_mix_in_schema_extra ():
62- """test that json_schema_extra is applied when it is in schema_extra"""
76+ """
77+ Test workaround when json_schema_extra was passed via schema_extra with Pydantic v2.
78+ """
6379
6480 with pytest .warns (DeprecationWarning , match = "schema_extra parameter is deprecated" ):
6581
0 commit comments