-
-
Notifications
You must be signed in to change notification settings - Fork 845
Expand file tree
/
Copy pathtest_field_json_schema_extra.py
More file actions
85 lines (63 loc) · 2.61 KB
/
test_field_json_schema_extra.py
File metadata and controls
85 lines (63 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pytest
from sqlmodel import Field, SQLModel
def test_json_schema_extra_applied():
"""test json_schema_extra is applied to the field"""
class Item(SQLModel):
name: str = Field(
json_schema_extra={
"example": "Sword of Power",
"x-custom-key": "Important Data",
}
)
schema = Item.model_json_schema()
name_schema = schema["properties"]["name"]
assert name_schema["example"] == "Sword of Power"
assert name_schema["x-custom-key"] == "Important Data"
def test_schema_extra_and_json_schema_extra_conflict():
"""
Test that passing schema_extra and json_schema_extra at the same time produces
a warning.
"""
with pytest.warns(DeprecationWarning, match="schema_extra parameter is deprecated"):
Field(schema_extra={"legacy": 1}, json_schema_extra={"new": 2})
def test_schema_extra_backward_compatibility():
"""
test that schema_extra is backward compatible with json_schema_extra
"""
with pytest.warns(DeprecationWarning, match="schema_extra parameter is deprecated"):
class LegacyItem(SQLModel):
name: str = Field(
schema_extra={
"example": "Sword of Old",
"x-custom-key": "Important Data",
"serialization_alias": "id_test",
}
)
schema = LegacyItem.model_json_schema()
name_schema = schema["properties"]["name"]
assert name_schema["example"] == "Sword of Old"
assert name_schema["x-custom-key"] == "Important Data"
# With Pydantic V1 serialization_alias from schema_extra is applied
field_info = LegacyItem.model_fields["name"]
assert field_info.serialization_alias == "id_test"
def test_json_schema_extra_mix_in_schema_extra():
"""
Test workaround when json_schema_extra was passed via schema_extra.
"""
with pytest.warns(DeprecationWarning, match="schema_extra parameter is deprecated"):
class Item(SQLModel):
name: str = Field(
schema_extra={
"json_schema_extra": {
"example": "Sword of Power",
"x-custom-key": "Important Data",
},
"serialization_alias": "id_test",
}
)
schema = Item.model_json_schema()
name_schema = schema["properties"]["name"]
assert name_schema["example"] == "Sword of Power"
assert name_schema["x-custom-key"] == "Important Data"
field_info = Item.model_fields["name"]
assert field_info.serialization_alias == "id_test"