Skip to content

Commit 4e3ce24

Browse files
authored
Merge pull request #2742 from bagerard/uuid_in_uri_fix
Accept uuidrepr in URI
2 parents b3434ab + c860098 commit 4e3ce24

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Changelog
77
Development
88
===========
99
- (Fill this out as you fix issues and develop your features).
10+
- Fix for uuidRepresentation not read when provided in URI #2741
1011

1112
Changes in 0.27.0
1213
=================

mongoengine/connection.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import warnings
22

33
from pymongo import MongoClient, ReadPreference, uri_parser
4+
from pymongo.common import (
5+
_UUID_REPRESENTATIONS,
6+
_CaseInsensitiveDictionary,
7+
)
48
from pymongo.database import _check_name
59

610
# DriverInfo was added in PyMongo 3.7.
@@ -131,7 +135,7 @@ def _get_connection_settings(
131135
if uri_dict.get(param):
132136
conn_settings[param] = uri_dict[param]
133137

134-
uri_options = uri_dict["options"]
138+
uri_options: _CaseInsensitiveDictionary = uri_dict["options"]
135139
if "replicaset" in uri_options:
136140
conn_settings["replicaSet"] = uri_options["replicaset"]
137141
if "authsource" in uri_options:
@@ -166,6 +170,13 @@ def _get_connection_settings(
166170
conn_settings["authmechanismproperties"] = uri_options[
167171
"authmechanismproperties"
168172
]
173+
if "uuidrepresentation" in uri_options:
174+
REV_UUID_REPRESENTATIONS = {
175+
v: k for k, v in _UUID_REPRESENTATIONS.items()
176+
}
177+
conn_settings["uuidrepresentation"] = REV_UUID_REPRESENTATIONS[
178+
uri_options["uuidrepresentation"]
179+
]
169180
else:
170181
resolved_hosts.append(entity)
171182
conn_settings["host"] = resolved_hosts
@@ -177,7 +188,7 @@ def _get_connection_settings(
177188
keys = {
178189
key.lower() for key in kwargs.keys()
179190
} # pymongo options are case insensitive
180-
if "uuidrepresentation" not in keys:
191+
if "uuidrepresentation" not in keys and "uuidrepresentation" not in conn_settings:
181192
warnings.warn(
182193
"No uuidRepresentation is specified! Falling back to "
183194
"'pythonLegacy' which is the default for pymongo 3.x. "

tests/test_connection.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import unittest
3+
import uuid
34

45
import pymongo
56
import pytest
@@ -30,6 +31,10 @@
3031
from mongoengine.pymongo_support import PYMONGO_VERSION
3132

3233

34+
def random_str():
35+
return str(uuid.uuid4())
36+
37+
3338
def get_tz_awareness(connection):
3439
return connection.codec_options.tz_aware
3540

@@ -624,6 +629,50 @@ def test_connect_2_databases_uses_different_client_if_different_parameters(self)
624629
c2 = connect(alias="testdb2", db="testdb2", username="u2", password="pass")
625630
assert c1 is not c2
626631

632+
def test_connect_uri_uuidrepresentation_set_in_uri(self):
633+
rand = random_str()
634+
tmp_conn = connect(
635+
alias=rand,
636+
host=f"mongodb://localhost:27017/{rand}?uuidRepresentation=csharpLegacy",
637+
)
638+
assert (
639+
tmp_conn.options.codec_options.uuid_representation
640+
== pymongo.common._UUID_REPRESENTATIONS["csharpLegacy"]
641+
)
642+
disconnect(rand)
643+
644+
def test_connect_uri_uuidrepresentation_set_as_arg(self):
645+
rand = random_str()
646+
tmp_conn = connect(alias=rand, db=rand, uuidRepresentation="javaLegacy")
647+
assert (
648+
tmp_conn.options.codec_options.uuid_representation
649+
== pymongo.common._UUID_REPRESENTATIONS["javaLegacy"]
650+
)
651+
disconnect(rand)
652+
653+
def test_connect_uri_uuidrepresentation_set_both_arg_and_uri_arg_prevail(self):
654+
rand = random_str()
655+
tmp_conn = connect(
656+
alias=rand,
657+
host=f"mongodb://localhost:27017/{rand}?uuidRepresentation=csharpLegacy",
658+
uuidRepresentation="javaLegacy",
659+
)
660+
assert (
661+
tmp_conn.options.codec_options.uuid_representation
662+
== pymongo.common._UUID_REPRESENTATIONS["javaLegacy"]
663+
)
664+
disconnect(rand)
665+
666+
def test_connect_uri_uuidrepresentation_default_to_pythonlegacy(self):
667+
# To be changed soon to unspecified
668+
rand = random_str()
669+
tmp_conn = connect(alias=rand, db=rand)
670+
assert (
671+
tmp_conn.options.codec_options.uuid_representation
672+
== pymongo.common._UUID_REPRESENTATIONS["pythonLegacy"]
673+
)
674+
disconnect(rand)
675+
627676

628677
if __name__ == "__main__":
629678
unittest.main()

0 commit comments

Comments
 (0)