Skip to content

Commit c2c8598

Browse files
committed
🐛 Fix certain situations
1 parent de70227 commit c2c8598

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

sqlmodel/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,19 @@ def Field(
453453
current_json_schema_extra = json_schema_extra or {}
454454
current_schema_extra = schema_extra or {}
455455

456-
if current_schema_extra:
456+
if IS_PYDANTIC_V2:
457457
for key, value in current_schema_extra.items():
458-
if key in FIELD_ACCEPTED_KWARGS:
458+
# if schema_extra={"json_schema_extra": {"x-yy-zz": "zz"}}
459+
if key == "json_schema_extra":
460+
current_json_schema_extra.update(value)
461+
elif key in FIELD_ACCEPTED_KWARGS:
459462
current_pydantic_kwargs[key] = value
460463
else:
461464
current_json_schema_extra[key] = value
465+
current_pydantic_kwargs["json_schema_extra"] = current_json_schema_extra
466+
else:
467+
current_pydantic_kwargs.update(current_json_schema_extra)
468+
current_pydantic_kwargs.update(current_schema_extra)
462469

463470
field_info = FieldInfo(
464471
default,
@@ -495,7 +502,6 @@ def Field(
495502
sa_column=sa_column,
496503
sa_column_args=sa_column_args,
497504
sa_column_kwargs=sa_column_kwargs,
498-
json_schema_extra=current_json_schema_extra,
499505
**current_pydantic_kwargs,
500506
)
501507
post_init_field_info(field_info)

tests/test_field_pd_and_json_kwarrgs.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,27 @@ class LegacyItem(SQLModel):
7171

7272
field_info = LegacyItem.model_fields["name"]
7373
assert field_info.serialization_alias == "id_test"
74+
75+
76+
def test_json_schema_extra_mix_in_schema_extra():
77+
"""test that json_schema_extra is applied when it is in schema_extra"""
78+
79+
class Item(SQLModel):
80+
name: str = Field(
81+
schema_extra={
82+
"json_schema_extra": {
83+
"example": "Sword of Power",
84+
"x-custom-key": "Important Data",
85+
},
86+
"serialization_alias": "id_test",
87+
}
88+
)
89+
90+
schema = Item.model_json_schema()
91+
92+
name_schema = schema["properties"]["name"]
93+
assert name_schema["example"] == "Sword of Power"
94+
assert name_schema["x-custom-key"] == "Important Data"
95+
96+
field_info = Item.model_fields["name"]
97+
assert field_info.serialization_alias == "id_test"

0 commit comments

Comments
 (0)