Skip to content

Commit 687b4a7

Browse files
authored
fix: pydantic 2.10.x breaking change (#1095)
* fix: breaking change with pydantic 2.10.x * chore: test on gha * fix: unify python and json validator schema * fix: revert version checking logic * fix: drop on push event on github actions * fix: marker logic for pydantic v2.10 * fix: add test pydantic 2.10.4
1 parent 81da389 commit 687b4a7

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

.github/workflows/github-actions-tests.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
name: Tests
2-
on: [ pull_request ]
2+
on:
3+
pull_request:
34

45
jobs:
56
pre-commit:
@@ -16,7 +17,7 @@ jobs:
1617
matrix:
1718
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12", "3.13" ]
1819
mongodb-version: [ "4.4", "5.0", "6.0", "7.0", "8.0" ]
19-
pydantic-version: [ "1.10.18", "2.9.2" ]
20+
pydantic-version: [ "1.10.18", "2.9.2" , "2.10.4"]
2021
runs-on: ubuntu-latest
2122
steps:
2223
- uses: actions/checkout@v4

beanie/odm/fields.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from beanie.odm.utils.parsing import parse_obj
3939
from beanie.odm.utils.pydantic import (
4040
IS_PYDANTIC_V2,
41+
IS_PYDANTIC_V2_10,
4142
get_field_type,
4243
get_model_fields,
4344
parse_object_as,
@@ -147,9 +148,8 @@ def _validate(cls, v):
147148
def __get_pydantic_core_schema__(
148149
cls, source_type: Type[Any], handler: GetCoreSchemaHandler
149150
) -> CoreSchema:
150-
return json_or_python_schema(
151-
python_schema=no_info_plain_validator_function(cls._validate),
152-
json_schema=no_info_plain_validator_function(
151+
if not IS_PYDANTIC_V2_10:
152+
return no_info_plain_validator_function(
153153
cls._validate,
154154
metadata={
155155
"pydantic_js_input_core_schema": str_schema(
@@ -158,6 +158,18 @@ def __get_pydantic_core_schema__(
158158
max_length=24,
159159
)
160160
},
161+
serialization=plain_serializer_function_ser_schema(
162+
lambda instance: str(instance),
163+
return_schema=str_schema(),
164+
when_used="json",
165+
),
166+
)
167+
return no_info_plain_validator_function(
168+
cls._validate,
169+
json_schema_input_schema=str_schema(
170+
pattern="^[0-9a-f]{24}$",
171+
min_length=24,
172+
max_length=24,
161173
),
162174
serialization=plain_serializer_function_ser_schema(
163175
lambda instance: str(instance),

beanie/odm/utils/encoder.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424

2525
import beanie
2626
from beanie.odm.fields import Link, LinkTypes
27-
from beanie.odm.utils.pydantic import IS_PYDANTIC_V2, get_model_fields
27+
from beanie.odm.utils.pydantic import (
28+
IS_PYDANTIC_V2,
29+
IS_PYDANTIC_V2_10,
30+
get_model_fields,
31+
)
2832

2933
SingleArgCallable = Callable[[Any], Any]
3034
DEFAULT_CUSTOM_ENCODERS: MutableMapping[type, SingleArgCallable] = {
@@ -51,6 +55,11 @@
5155

5256
DEFAULT_CUSTOM_ENCODERS[Url] = str
5357

58+
if IS_PYDANTIC_V2_10:
59+
from pydantic import AnyUrl
60+
61+
DEFAULT_CUSTOM_ENCODERS[AnyUrl] = str
62+
5463
BSON_SCALAR_TYPES = (
5564
type(None),
5665
str,

beanie/odm/utils/pydantic.py

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
from pydantic import BaseModel
55

66
IS_PYDANTIC_V2 = int(pydantic.VERSION.split(".")[0]) >= 2
7+
IS_PYDANTIC_V2_10 = (
8+
IS_PYDANTIC_V2 and int(pydantic.VERSION.split(".")[1]) >= 10
9+
)
710

811
if IS_PYDANTIC_V2:
912
from pydantic import TypeAdapter

0 commit comments

Comments
 (0)