-
-
Notifications
You must be signed in to change notification settings - Fork 781
✨ Add json_schema_extra and deprecate schema_extra
#1035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KimigaiiWuyi
wants to merge
21
commits into
fastapi:main
Choose a base branch
from
KimigaiiWuyi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−6
Open
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ecfafa2
🎨 `Field()` supports `json_schema_extra` for `Pydantic v2`
KimigaiiWuyi c9f7ad8
Merge branch 'main' into main
KimigaiiWuyi 60bd15a
🐛 Fix potential conflicts
KimigaiiWuyi 750d1d0
Merge branch 'main' into main
svlandeg a6497b4
Merge branch 'fastapi:main' into main
KimigaiiWuyi 4595d70
🎨 Increase unit test and improve compatibility
KimigaiiWuyi 7912785
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] f0ae20e
🔥 Remove comments
KimigaiiWuyi 0e48a37
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 249ad47
🎨 Ensure compatibility
KimigaiiWuyi 42c2a9b
🔥 Remove the test configuration file
KimigaiiWuyi 3380fb0
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] fb63890
Merge branch 'main' into main
YuriiMotov 8314d1d
🎨 Update Deprecation Notice
KimigaiiWuyi de70227
🎨 Update test cases
KimigaiiWuyi c2c8598
🐛 Fix certain situations
KimigaiiWuyi 522b030
Remove `pydantic_kwargs`, fix backward compatibility, fix tests
YuriiMotov 497aeb1
Rename test file, fix tests for Pydantic V1
YuriiMotov 52ea0df
Merge branch 'main' into main
YuriiMotov 3ad8aa9
Remove unused caplog parameter in tests
YuriiMotov 20188bf
Always remove `json_schema_extra` from `schema_extra` with Pydantic V2
YuriiMotov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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_pydantic_kwargs_applied(): | ||
| """test pydantic_kwargs is applied to the field""" | ||
|
|
||
| class User(SQLModel): | ||
| user_name: str = Field(pydantic_kwargs={"validation_alias": "UserNameInInput"}) | ||
|
|
||
| field_info = User.model_fields["user_name"] | ||
|
|
||
| assert field_info.validation_alias == "UserNameInInput" | ||
|
|
||
| data = {"UserNameInInput": "KimigaiiWuyi"} | ||
| user = User.model_validate(data) | ||
| assert user.user_name == "KimigaiiWuyi" | ||
|
|
||
|
|
||
| def test_schema_extra_and_new_param_conflict(): | ||
| """test that passing schema_extra and new params at the same time raises an error""" | ||
|
|
||
| with pytest.raises(RuntimeError) as excinfo: | ||
|
|
||
| class ItemA(SQLModel): | ||
| name: str = Field( | ||
| schema_extra={"legacy": 1}, | ||
| json_schema_extra={"new": 2}, | ||
| ) | ||
|
|
||
| assert "Passing schema_extra is not supported" in str(excinfo.value) | ||
|
|
||
| with pytest.raises(RuntimeError) as excinfo: | ||
|
|
||
| class ItemB(SQLModel): | ||
| name: str = Field( | ||
| schema_extra={"legacy": 1}, | ||
| pydantic_kwargs={"alias": "Alias"}, | ||
| ) | ||
|
|
||
| assert "Passing schema_extra is not supported" in str(excinfo.value) | ||
KimigaiiWuyi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_schema_extra_backward_compatibility(): | ||
| """ | ||
| test that schema_extra is backward compatible with json_schema_extra | ||
| """ | ||
|
|
||
| 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" | ||
|
|
||
| field_info = LegacyItem.model_fields["name"] | ||
| assert field_info.serialization_alias == "id_test" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.