Repository: MongoEngine/mongoengine

Issue:
haven't read the `uuidRepresentation` in uri
file path: mongoengine/connection.py line 120
According to the official library handling, the uuid option should allow `uuidRepresentation` setting in uri, however, mongoengine does not read it.
example uri: `mongodb://localhost:27017/test?uuidRepresentation=standard`


Discussion:
Good catch, working on it now

Resolving patch:
diff --git a/mongoengine/connection.py b/mongoengine/connection.py
--- a/mongoengine/connection.py
+++ b/mongoengine/connection.py
@@ -1,6 +1,10 @@
 import warnings
 
 from pymongo import MongoClient, ReadPreference, uri_parser
+from pymongo.common import (
+    _UUID_REPRESENTATIONS,
+    _CaseInsensitiveDictionary,
+)
 from pymongo.database import _check_name
 
 from mongoengine.pymongo_support import PYMONGO_VERSION
@@ -124,7 +128,7 @@ def _get_connection_settings(
                 if uri_dict.get(param):
                     conn_settings[param] = uri_dict[param]
 
-            uri_options = uri_dict["options"]
+            uri_options: _CaseInsensitiveDictionary = uri_dict["options"]
             if "replicaset" in uri_options:
                 conn_settings["replicaSet"] = uri_options["replicaset"]
             if "authsource" in uri_options:
@@ -159,6 +163,13 @@ def _get_connection_settings(
                 conn_settings["authmechanismproperties"] = uri_options[
                     "authmechanismproperties"
                 ]
+            if "uuidrepresentation" in uri_options:
+                REV_UUID_REPRESENTATIONS = {
+                    v: k for k, v in _UUID_REPRESENTATIONS.items()
+                }
+                conn_settings["uuidrepresentation"] = REV_UUID_REPRESENTATIONS[
+                    uri_options["uuidrepresentation"]
+                ]
         else:
             resolved_hosts.append(entity)
     conn_settings["host"] = resolved_hosts
@@ -170,7 +181,7 @@ def _get_connection_settings(
     keys = {
         key.lower() for key in kwargs.keys()
     }  # pymongo options are case insensitive
-    if "uuidrepresentation" not in keys:
+    if "uuidrepresentation" not in keys and "uuidrepresentation" not in conn_settings:
         warnings.warn(
             "No uuidRepresentation is specified! Falling back to "
             "'pythonLegacy' which is the default for pymongo 3.x. "


Test patch:
diff --git a/tests/test_connection.py b/tests/test_connection.py
--- a/tests/test_connection.py
+++ b/tests/test_connection.py
@@ -1,5 +1,6 @@
 import datetime
 import unittest
+import uuid
 
 import pymongo
 import pytest
@@ -30,6 +31,10 @@
 from mongoengine.pymongo_support import PYMONGO_VERSION
 
 
+def random_str():
+    return str(uuid.uuid4())
+
+
 def get_tz_awareness(connection):
     return connection.codec_options.tz_aware
 
@@ -624,6 +629,50 @@ def test_connect_2_databases_uses_different_client_if_different_parameters(self)
         c2 = connect(alias="testdb2", db="testdb2", username="u2", password="pass")
         assert c1 is not c2
 
+    def test_connect_uri_uuidrepresentation_set_in_uri(self):
+        rand = random_str()
+        tmp_conn = connect(
+            alias=rand,
+            host=f"mongodb://localhost:27017/{rand}?uuidRepresentation=csharpLegacy",
+        )
+        assert (
+            tmp_conn.options.codec_options.uuid_representation
+            == pymongo.common._UUID_REPRESENTATIONS["csharpLegacy"]
+        )
+        disconnect(rand)
+
+    def test_connect_uri_uuidrepresentation_set_as_arg(self):
+        rand = random_str()
+        tmp_conn = connect(alias=rand, db=rand, uuidRepresentation="javaLegacy")
+        assert (
+            tmp_conn.options.codec_options.uuid_representation
+            == pymongo.common._UUID_REPRESENTATIONS["javaLegacy"]
+        )
+        disconnect(rand)
+
+    def test_connect_uri_uuidrepresentation_set_both_arg_and_uri_arg_prevail(self):
+        rand = random_str()
+        tmp_conn = connect(
+            alias=rand,
+            host=f"mongodb://localhost:27017/{rand}?uuidRepresentation=csharpLegacy",
+            uuidRepresentation="javaLegacy",
+        )
+        assert (
+            tmp_conn.options.codec_options.uuid_representation
+            == pymongo.common._UUID_REPRESENTATIONS["javaLegacy"]
+        )
+        disconnect(rand)
+
+    def test_connect_uri_uuidrepresentation_default_to_pythonlegacy(self):
+        # To be changed soon to unspecified
+        rand = random_str()
+        tmp_conn = connect(alias=rand, db=rand)
+        assert (
+            tmp_conn.options.codec_options.uuid_representation
+            == pymongo.common._UUID_REPRESENTATIONS["pythonLegacy"]
+        )
+        disconnect(rand)
+
 
 if __name__ == "__main__":
     unittest.main()