-
-
Notifications
You must be signed in to change notification settings - Fork 791
🐛 Fix alias support for Pydantic v2
#1577
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
Merged
tiangolo
merged 18 commits into
fastapi:main
from
ravishan16:issue-1536-alias-pydantic-v2
Dec 23, 2025
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
785270a
feat: Add support for Pydantic v2 aliases
ravishan16 72e8c36
fix: Skip alias tests in Pydantic v1
ravishan16 15c7954
feat: Add support for Pydantic v2 aliases
ravishan16 3cc2b1e
fix: Skip alias tests in Pydantic v1
ravishan16 34d2a45
fix: Add Pydantic v2 alias support with backward compatibility
ravishan16 6667109
Apply suggestion from @YuriiMotov
ravishan16 382a52d
fix: move this outside of if..else.. block to avoid duplicati as per …
ravishan16 d3e761e
fix: Addressed yurii's comments
ravishan16 e3dbf75
Merge branch 'fastapi:main' into issue-1536-alias-pydantic-v2
ravishan16 e07a259
Update tests/test_aliases.py
ravishan16 6b7a0a1
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
pre-commit-ci[bot] 322c6af
Field: support schema_extra aliases (v2); add alias tests
ravishan16 6b13381
Field: support schema_extra aliases (v2); add alias tests
ravishan16 7b2bc2c
compat: drop redundant v2 check in post_init_field_info;
ravishan16 68b31d7
Simplify alias propagation logic for Pydantic v2
YuriiMotov 9440956
Fix test name and avoid deprecation warnings
YuriiMotov 84cd604
Merge branch 'main' into issue-1536-alias-pydantic-v2
tiangolo 3ba0e8e
✅ Tweak test to use Pydantic v1 compat utils
tiangolo 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,257 @@ | ||
| from typing import Type, Union | ||
|
|
||
| import pytest | ||
| from pydantic import BaseModel, ValidationError | ||
| from pydantic import Field as PField | ||
| from sqlmodel import Field, SQLModel | ||
| from sqlmodel._compat import IS_PYDANTIC_V2 | ||
|
|
||
| from tests.conftest import needs_pydanticv1, needs_pydanticv2 | ||
|
|
||
| """ | ||
| Alias tests for SQLModel and Pydantic compatibility | ||
| """ | ||
|
|
||
|
|
||
| class PydanticUser(BaseModel): | ||
tiangolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| full_name: str = PField(alias="fullName") | ||
|
|
||
|
|
||
| class SQLModelUser(SQLModel): | ||
| full_name: str = Field(alias="fullName") | ||
|
|
||
|
|
||
| # Models with config (validate_by_name=True) | ||
| if IS_PYDANTIC_V2: | ||
|
|
||
| class PydanticUserWithConfig(PydanticUser): | ||
| model_config = {"validate_by_name": True} | ||
|
|
||
| class SQLModelUserWithConfig(SQLModelUser): | ||
| model_config = {"validate_by_name": True} | ||
|
|
||
| else: | ||
|
|
||
| class PydanticUserWithConfig(PydanticUser): | ||
| class Config: | ||
| allow_population_by_field_name = True | ||
|
|
||
| class SQLModelUserWithConfig(SQLModelUser): | ||
| class Config: | ||
| allow_population_by_field_name = True | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||
| def test_create_with_field_name(model: Union[Type[PydanticUser], Type[SQLModelUser]]): | ||
| with pytest.raises(ValidationError): | ||
| model(full_name="Alice") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUserWithConfig, SQLModelUserWithConfig]) | ||
| def test_create_with_field_name_with_config( | ||
| model: Union[Type[PydanticUserWithConfig], Type[SQLModelUserWithConfig]], | ||
| ): | ||
| user = model(full_name="Alice") | ||
| assert user.full_name == "Alice" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "model", | ||
| [PydanticUser, SQLModelUser, PydanticUserWithConfig, SQLModelUserWithConfig], | ||
| ) | ||
| def test_create_with_alias( | ||
| model: Union[ | ||
| Type[PydanticUser], | ||
| Type[SQLModelUser], | ||
| Type[PydanticUserWithConfig], | ||
| Type[SQLModelUserWithConfig], | ||
| ], | ||
| ): | ||
| user = model(fullName="Bob") # using alias | ||
| assert user.full_name == "Bob" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUserWithConfig, SQLModelUserWithConfig]) | ||
| def test_create_with_both_prefers_alias( | ||
| model: Union[Type[PydanticUserWithConfig], Type[SQLModelUserWithConfig]], | ||
| ): | ||
| user = model(full_name="IGNORED", fullName="Charlie") | ||
| assert user.full_name == "Charlie" # alias should take precedence | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||
| def test_dict_default_uses_field_names( | ||
| model: Union[Type[PydanticUser], Type[SQLModelUser]], | ||
| ): | ||
| user = model(fullName="Dana") | ||
| if IS_PYDANTIC_V2 or isinstance(user, SQLModel): | ||
| data = user.model_dump() | ||
| else: | ||
| data = user.dict() | ||
| assert "full_name" in data | ||
| assert "fullName" not in data | ||
| assert data["full_name"] == "Dana" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||
| def test_dict_by_alias_uses_aliases( | ||
| model: Union[Type[PydanticUser], Type[SQLModelUser]], | ||
| ): | ||
| user = model(fullName="Dana") | ||
| if IS_PYDANTIC_V2 or isinstance(user, SQLModel): | ||
| data = user.model_dump(by_alias=True) | ||
| else: | ||
| data = user.dict(by_alias=True) | ||
| assert "fullName" in data | ||
| assert "full_name" not in data | ||
| assert data["fullName"] == "Dana" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("model", [PydanticUser, SQLModelUser]) | ||
| def test_json_by_alias( | ||
| model: Union[Type[PydanticUser], Type[SQLModelUser]], | ||
| ): | ||
| user = model(fullName="Frank") | ||
| if IS_PYDANTIC_V2: | ||
| json_data = user.model_dump_json(by_alias=True) | ||
| else: | ||
| json_data = user.json(by_alias=True) | ||
| assert ('"fullName":"Frank"' in json_data) or ('"fullName": "Frank"' in json_data) | ||
| assert "full_name" not in json_data | ||
|
|
||
|
|
||
| if IS_PYDANTIC_V2: | ||
|
|
||
| class PydanticUserV2(BaseModel): | ||
| first_name: str = PField( | ||
| validation_alias="firstName", serialization_alias="f_name" | ||
| ) | ||
|
|
||
| class SQLModelUserV2(SQLModel): | ||
| first_name: str = Field( | ||
| validation_alias="firstName", serialization_alias="f_name" | ||
| ) | ||
| else: | ||
| # Dummy classes for Pydantic v1 to prevent import errors | ||
| PydanticUserV2 = None | ||
| SQLModelUserV2 = None | ||
YuriiMotov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @needs_pydanticv1 | ||
| def test_validation_alias_runtimeerror_pydantic_v1(): | ||
| with pytest.raises( | ||
| RuntimeError, match="validation_alias is not supported in Pydantic v1" | ||
| ): | ||
| Field(validation_alias="foo") | ||
|
|
||
|
|
||
| @needs_pydanticv1 | ||
| def test_serialization_alias_runtimeerror_pydantic_v1(): | ||
| with pytest.raises( | ||
| RuntimeError, match="serialization_alias is not supported in Pydantic v1" | ||
| ): | ||
| Field(serialization_alias="bar") | ||
|
|
||
|
|
||
| @needs_pydanticv2 | ||
| @pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2]) | ||
| def test_create_with_validation_alias( | ||
| model: Union[Type[PydanticUserV2], Type[SQLModelUserV2]], | ||
| ): | ||
| user = model(firstName="John") | ||
| assert user.first_name == "John" | ||
|
|
||
|
|
||
| @needs_pydanticv2 | ||
| @pytest.mark.parametrize("model", [PydanticUserV2, SQLModelUserV2]) | ||
| def test_serialize_with_serialization_alias( | ||
| model: Union[Type[PydanticUserV2], Type[SQLModelUserV2]], | ||
| ): | ||
| user = model(firstName="Jane") | ||
| data = user.model_dump(by_alias=True) | ||
| assert "f_name" in data | ||
| assert "firstName" not in data | ||
| assert "first_name" not in data | ||
| assert data["f_name"] == "Jane" | ||
|
|
||
|
|
||
| @needs_pydanticv2 | ||
| def test_schema_extra_validation_alias_sqlmodel_v2(): | ||
| class M(SQLModel): | ||
| f: str = Field(schema_extra={"validation_alias": "f_alias"}) | ||
|
|
||
| m = M.model_validate({"f_alias": "asd"}) | ||
| assert m.f == "asd" | ||
|
|
||
|
|
||
| @needs_pydanticv2 | ||
| def test_schema_extra_serialization_alias_sqlmodel_v2(): | ||
| class M(SQLModel): | ||
| f: str = Field(schema_extra={"serialization_alias": "f_out"}) | ||
|
|
||
| m = M(f="x") | ||
| data = m.model_dump(by_alias=True) | ||
| assert "f_out" in data | ||
| assert "f" not in data | ||
| assert data["f_out"] == "x" | ||
|
|
||
|
|
||
| @needs_pydanticv1 | ||
| def test_schema_extra_validation_alias_runtimeerror_pydantic_v1(): | ||
| with pytest.raises( | ||
| RuntimeError, match="validation_alias is not supported in Pydantic v1" | ||
| ): | ||
| Field(schema_extra={"validation_alias": "x"}) | ||
|
|
||
|
|
||
| @needs_pydanticv1 | ||
| def test_schema_extra_serialization_alias_runtimeerror_pydantic_v1(): | ||
| with pytest.raises( | ||
| RuntimeError, match="serialization_alias is not supported in Pydantic v1" | ||
| ): | ||
| Field(schema_extra={"serialization_alias": "y"}) | ||
|
|
||
|
|
||
| @needs_pydanticv2 | ||
| def test_alias_plus_validation_alias_prefers_validation_alias_sqlmodel_v2(): | ||
| class M(SQLModel): | ||
| first_name: str = Field(alias="fullName", validation_alias="v_name") | ||
|
|
||
| m = M.model_validate({"fullName": "A", "v_name": "B"}) | ||
| assert m.first_name == "B" | ||
|
|
||
|
|
||
| @needs_pydanticv2 | ||
| def test_alias_plus_serialization_alias_prefers_serialization_alias_sqlmodel_v2(): | ||
| class M(SQLModel): | ||
| first_name: str = Field(alias="fullName", serialization_alias="f_name") | ||
|
|
||
| m = M(fullName="Z") | ||
| data = m.model_dump(by_alias=True) | ||
| assert "f_name" in data | ||
| assert "fullName" not in data | ||
| assert data["f_name"] == "Z" | ||
|
|
||
|
|
||
| @needs_pydanticv2 | ||
| def test_alias_generator_works_sqlmodel_v2(): | ||
| class M(SQLModel): | ||
| model_config = {"alias_generator": lambda s: "gen_" + s} | ||
| f: str = Field() | ||
|
|
||
| m = M.model_validate({"gen_f": "ok"}) | ||
| assert m.f == "ok" | ||
| data = m.model_dump(by_alias=True) | ||
| assert "gen_f" in data and data["gen_f"] == "ok" | ||
|
|
||
|
|
||
| @needs_pydanticv2 | ||
| def test_alias_generator_with_explicit_alias_prefers_field_alias_sqlmodel_v2(): | ||
| class M(SQLModel): | ||
| model_config = {"alias_generator": lambda s: "gen_" + s} | ||
| f: str = Field(alias="custom") | ||
|
|
||
| m = M.model_validate({"custom": "ok"}) | ||
| assert m.f == "ok" | ||
| data = m.model_dump(by_alias=True) | ||
| assert "custom" in data and "gen_f" not in data | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pydantic's
validation_aliastype annotation is wider (validation_alias: str | AliasPath | AliasChoices | None), but I think it's fine if we only supportstrfor now and extend it laterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong preference on this.