Skip to content

Commit

Permalink
fix #983
Browse files Browse the repository at this point in the history
  • Loading branch information
07pepa authored and 07pepa committed Aug 27, 2024
1 parent bdb6a86 commit ecd3397
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
26 changes: 26 additions & 0 deletions beanie/odm/custom_types/re.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import re

import bson
import pydantic
from typing_extensions import Annotated

from beanie.odm.utils.pydantic import IS_PYDANTIC_V2


def _to_bson_regex(v):
return v.try_compile() if isinstance(v, bson.Regex) else v


if IS_PYDANTIC_V2:
Pattern = Annotated[
re.Pattern,
pydantic.BeforeValidator(
lambda v: v.try_compile() if isinstance(v, bson.Regex) else v
),
]
else:

class Pattern(bson.Regex): # type: ignore[no-redef]
@classmethod
def __get_validators__(cls):
yield _to_bson_regex
1 change: 1 addition & 0 deletions beanie/odm/utils/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
bson.MaxKey,
bson.MinKey,
bson.ObjectId,
bson.Regex,
)


Expand Down
5 changes: 5 additions & 0 deletions tests/odm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
BDocument,
Bicycle,
Bike,
BsonRegexDoc,
Bus,
Car,
Doc2NonRoot,
Expand Down Expand Up @@ -82,6 +83,7 @@
LongSelfLink,
LoopedLinksA,
LoopedLinksB,
NativeRegexDoc,
Nested,
Option1,
Option2,
Expand Down Expand Up @@ -291,7 +293,10 @@ async def init(db):
DocumentWithLinkForNesting,
DocumentWithBackLinkForNesting,
LongSelfLink,
BsonRegexDoc,
NativeRegexDoc,
]

await init_beanie(
database=db,
document_models=models,
Expand Down
13 changes: 13 additions & 0 deletions tests/odm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from uuid import UUID, uuid4

import pymongo
from bson import Regex
from pydantic import (
UUID4,
BaseModel,
Expand Down Expand Up @@ -52,6 +53,7 @@
ValidateOnSave,
)
from beanie.odm.actions import Delete, after_event, before_event
from beanie.odm.custom_types import re
from beanie.odm.custom_types.bson.binary import BsonBinary
from beanie.odm.fields import BackLink, Link, PydanticObjectId
from beanie.odm.settings.timeseries import TimeSeriesConfig
Expand Down Expand Up @@ -1139,3 +1141,14 @@ class LongSelfLink(Document):

class Settings:
max_nesting_depth = 50


class BsonRegexDoc(Document):
regex: Optional[Regex] = None

class Config:
arbitrary_types_allowed = True


class NativeRegexDoc(Document):
regex: Optional[re.Pattern]
18 changes: 18 additions & 0 deletions tests/odm/test_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from beanie.odm.utils.encoder import Encoder
from beanie.odm.utils.pydantic import IS_PYDANTIC_V2
from tests.odm.models import (
BsonRegexDoc,
Child,
DocumentForEncodingTest,
DocumentForEncodingTestDate,
Expand All @@ -18,6 +19,7 @@
DocumentWithKeepNullsFalse,
DocumentWithStringField,
ModelWithOptionalField,
NativeRegexDoc,
SampleWithMutableObjects,
)

Expand Down Expand Up @@ -169,3 +171,19 @@ async def test_dict_with_complex_key():

assert isinstance(new_doc.dict_field, dict)
assert new_doc.dict_field.get(uuid) == dt


async def test_native_regex():
regex = re.compile(r"^1?$|^(11+?)\1+$", (re.I | re.M | re.S) ^ re.UNICODE)
doc = await NativeRegexDoc(regex=regex).insert()
new_doc = await NativeRegexDoc.get(doc.id)
assert new_doc.regex == regex
assert new_doc.regex.pattern == r"^1?$|^(11+?)\1+$"
assert new_doc.regex.flags == int(re.I | re.M | re.S ^ re.UNICODE)


async def test_bson_regex():
regex = Regex(r"^1?$|^(11+?)\1+$")
doc = await BsonRegexDoc(regex=regex).insert()
new_doc = await BsonRegexDoc.get(doc.id)
assert new_doc.regex == Regex(pattern=r"^1?$|^(11+?)\1+$")

0 comments on commit ecd3397

Please sign in to comment.