Skip to content

Commit f930aab

Browse files
committed
feat: use legacy python UUID encoding by default if unspecified
1 parent 56bba1a commit f930aab

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

mongoengine/base/document.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import numbers
3+
import warnings
34
from functools import partial
45

56
import pymongo
@@ -23,6 +24,7 @@
2324
OperationError,
2425
ValidationError,
2526
)
27+
from mongoengine.pymongo_support import LEGACY_JSON_OPTIONS
2628

2729
__all__ = ("BaseDocument", "NON_FIELD_ERRORS")
2830

@@ -444,10 +446,19 @@ def to_json(self, *args, **kwargs):
444446
Defaults to True.
445447
"""
446448
use_db_field = kwargs.pop("use_db_field", True)
449+
if "json_options" not in kwargs:
450+
warnings.warn(
451+
"No 'json_options' are specified! Falling back to "
452+
"LEGACY_JSON_OPTIONS with uuid_representation=PYTHON_LEGACY. "
453+
"For use with other MongoDB drivers specify the UUID "
454+
"representation to use.",
455+
DeprecationWarning,
456+
)
457+
kwargs["json_options"] = LEGACY_JSON_OPTIONS
447458
return json_util.dumps(self.to_mongo(use_db_field), *args, **kwargs)
448459

449460
@classmethod
450-
def from_json(cls, json_data, created=False):
461+
def from_json(cls, json_data, created=False, **kwargs):
451462
"""Converts json data to a Document instance
452463
453464
:param str json_data: The json data to load into the Document
@@ -465,7 +476,16 @@ def from_json(cls, json_data, created=False):
465476
# TODO should `created` default to False? If the object already exists
466477
# in the DB, you would likely retrieve it from MongoDB itself through
467478
# a query, not load it from JSON data.
468-
return cls._from_son(json_util.loads(json_data), created=created)
479+
if "json_options" not in kwargs:
480+
warnings.warn(
481+
"No 'json_options' are specified! Falling back to "
482+
"LEGACY_JSON_OPTIONS with uuid_representation=PYTHON_LEGACY. "
483+
"For use with other MongoDB drivers specify the UUID "
484+
"representation to use.",
485+
DeprecationWarning,
486+
)
487+
kwargs["json_options"] = LEGACY_JSON_OPTIONS
488+
return cls._from_son(json_util.loads(json_data, **kwargs), created=created)
469489

470490
def __expand_dynamic_values(self, name, value):
471491
"""Expand any dynamic values to their correct types / values."""

mongoengine/connection.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
from pymongo import MongoClient, ReadPreference, uri_parser
24
from pymongo.database import _check_name
35

@@ -162,6 +164,18 @@ def _get_connection_settings(
162164
kwargs.pop("slaves", None)
163165
kwargs.pop("is_slave", None)
164166

167+
if "uuidRepresentation" not in kwargs:
168+
warnings.warn(
169+
"No uuidRepresentation is specified! Falling back to "
170+
"'pythonLegacy' which is the default for pymongo 3.x. "
171+
"For compatibility with other MongoDB drivers this should be "
172+
"specified as 'standard' or '{java,csharp}Legacy' to work with "
173+
"older drivers in those languages. This will be changed to "
174+
"'standard' in a future release.",
175+
DeprecationWarning,
176+
)
177+
kwargs["uuidRepresentation"] = "pythonLegacy"
178+
165179
conn_settings.update(kwargs)
166180
return conn_settings
167181

mongoengine/pymongo_support.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
"""
2-
Helper functions, constants, and types to aid with PyMongo v2.7 - v3.x support.
2+
Helper functions, constants, and types to aid with PyMongo support.
33
"""
44
import pymongo
5+
from bson import binary, json_util
56
from pymongo.errors import OperationFailure
67

78
PYMONGO_VERSION = tuple(pymongo.version_tuple[:2])
89

10+
LEGACY_JSON_OPTIONS = json_util.LEGACY_JSON_OPTIONS.with_options(
11+
uuid_representation=binary.UuidRepresentation.PYTHON_LEGACY,
12+
)
13+
914

1015
def count_documents(
1116
collection, filter, skip=None, limit=None, hint=None, collation=None

mongoengine/queryset/base.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
NotUniqueError,
2929
OperationError,
3030
)
31-
from mongoengine.pymongo_support import count_documents
31+
from mongoengine.pymongo_support import (
32+
LEGACY_JSON_OPTIONS,
33+
count_documents,
34+
)
3235
from mongoengine.queryset import transform
3336
from mongoengine.queryset.field_list import QueryFieldList
3437
from mongoengine.queryset.visitor import Q, QNode
@@ -1266,6 +1269,15 @@ def max_time_ms(self, ms):
12661269

12671270
def to_json(self, *args, **kwargs):
12681271
"""Converts a queryset to JSON"""
1272+
if "json_options" not in kwargs:
1273+
warnings.warn(
1274+
"No 'json_options' are specified! Falling back to "
1275+
"LEGACY_JSON_OPTIONS with uuid_representation=PYTHON_LEGACY. "
1276+
"For use with other MongoDB drivers specify the UUID "
1277+
"representation to use.",
1278+
DeprecationWarning,
1279+
)
1280+
kwargs["json_options"] = LEGACY_JSON_OPTIONS
12691281
return json_util.dumps(self.as_pymongo(), *args, **kwargs)
12701282

12711283
def from_json(self, json_data):

0 commit comments

Comments
 (0)