Skip to content

Commit 75cf976

Browse files
committed
Add support for pydantic_settings.BaseSettings in dependencies #250
1 parent 6cd3d3a commit 75cf976

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Please follow [the Keep a Changelog standard](https://keepachangelog.com/en/1.0.
55

66
## [Unreleased]
77

8+
## [5.3.2]
9+
10+
### Fixed
11+
12+
* Add support for `pydantic_settings.BaseSettings` in dependencies
13+
814
## [5.3.1]
915

1016
### Fixed

cadwyn/schema_generation.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@
102102
ComputedFieldInfo: pydantic.computed_field,
103103
}
104104
_PYDANTIC_ALL_EXPORTED_NAMES = set(pydantic.__all__)
105+
_DEFAULT_PYDANTIC_CLASSES = (BaseModel, RootModel)
106+
107+
try:
108+
from pydantic_settings import BaseSettings
109+
110+
_DEFAULT_PYDANTIC_CLASSES = (*_DEFAULT_PYDANTIC_CLASSES, BaseSettings)
111+
except ImportError: # pragma: no cover
112+
pass
105113

106114

107115
VALIDATOR_CONFIG_KEY = "__validators__"
@@ -757,7 +765,7 @@ def __getitem__(self, model: type[_T_ANY_MODEL], /) -> type[_T_ANY_MODEL]:
757765
if (
758766
not isinstance(model, type)
759767
or not lenient_issubclass(model, (BaseModel, Enum))
760-
or model in (BaseModel, RootModel)
768+
or model in _DEFAULT_PYDANTIC_CLASSES
761769
):
762770
return model
763771
model = _unwrap_model(model)

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "cadwyn"
3-
version = "5.3.1"
3+
version = "5.3.2"
44
description = "Production-ready community-driven modern Stripe-like API versioning in FastAPI"
55
authors = [{ name = "Stanislav Zmiev", email = "zmievsa@gmail.com" }]
66
license = "MIT"
@@ -88,6 +88,7 @@ dev-dependencies = [
8888
"pdbpp>=0.10.3",
8989
"markdown-include-variants>=0.0.4",
9090
"inline-snapshot>=0.20.7",
91+
"pydantic-settings>=2.8.1",
9192
]
9293

9394
[project.urls]

tests/test_router_generation.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from fastapi.security.http import HTTPBasic
1515
from fastapi.testclient import TestClient
1616
from inline_snapshot import snapshot
17-
from pydantic import BaseModel, JsonValue
17+
from pydantic import BaseModel, Field, JsonValue
18+
from pydantic_settings import BaseSettings
1819
from pytest_fixture_classes import fixture_class
1920
from starlette.responses import FileResponse
2021
from typing_extensions import Any, NewType, TypeAlias, TypeAliasType, get_args
@@ -1355,6 +1356,32 @@ async def test(
13551356
)
13561357

13571358

1359+
def test__router_generation__with_pydantic_settings_dependency(
1360+
create_versioned_app: CreateVersionedApp,
1361+
router: VersionedAPIRouter,
1362+
) -> None:
1363+
class Settings(BaseSettings):
1364+
foo: str = Field(default="bar")
1365+
1366+
async def settings() -> Settings:
1367+
return Settings()
1368+
1369+
@router.get("/test")
1370+
def read_root(settings: Annotated[Settings, Depends(settings)]):
1371+
assert settings
1372+
return {"foo": settings.foo}
1373+
1374+
app = create_versioned_app(version_change())
1375+
with TestClient(app) as client:
1376+
resp = client.get("/test", headers={"x-api-version": "2000-01-01"})
1377+
assert resp.status_code == 200
1378+
assert resp.json() == {"foo": "bar"}
1379+
1380+
resp = client.get("/test", headers={"x-api-version": "2001-01-01"})
1381+
assert resp.status_code == 200
1382+
assert resp.json() == {"foo": "bar"}
1383+
1384+
13581385
######################
13591386
# External lib testing
13601387
######################

uv.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)