Skip to content

Commit

Permalink
fix(pydantic): Add moved URL types to type map (#3874)
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut authored Nov 24, 2024
1 parent 58ae5d9 commit 06ce3a0
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 285 deletions.
2 changes: 1 addition & 1 deletion litestar/middleware/_internal/exceptions/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _starlette_exception_handler(request: Request[Any, Any, Any], exc: Starlette
exc=HTTPException(
detail=exc.detail,
status_code=exc.status_code,
headers=exc.headers,
headers=exc.headers, # type: ignore[arg-type]
),
)

Expand Down
12 changes: 12 additions & 0 deletions litestar/plugins/pydantic/plugins/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@
}

if pydantic_v2 is not None: # pragma: no cover
from pydantic import networks

PYDANTIC_TYPE_MAP.update(
{
pydantic_v2.SecretStr: Schema(type=OpenAPIType.STRING),
Expand Down Expand Up @@ -194,6 +196,16 @@
pydantic_v2.AnyUrl: Schema(type=OpenAPIType.STRING, format=OpenAPIFormat.URL),
}
)
if int(pydantic_v2.version.version_short().split(".")[1]) >= 10:
# These were 'Annotated' type aliases before Pydantic 2.10, where they were
# changed to proper classes. Using subscripted generics type in an 'isinstance'
# check would raise a 'TypeError' on Python <3.12
PYDANTIC_TYPE_MAP.update(
{
networks.HttpUrl: Schema(type=OpenAPIType.STRING, format=OpenAPIFormat.URL),
networks.AnyHttpUrl: Schema(type=OpenAPIType.STRING, format=OpenAPIFormat.URL),
}
)


_supported_types = (pydantic_v1.BaseModel, *PYDANTIC_TYPE_MAP.keys())
Expand Down
Loading

0 comments on commit 06ce3a0

Please sign in to comment.