Skip to content

Commit 6b1bef3

Browse files
authored
PYTHON-3962 Make delimiting slash between hosts and options optional (#1404)
1 parent a0e9d61 commit 6b1bef3

File tree

4 files changed

+57
-23
lines changed

4 files changed

+57
-23
lines changed

doc/changelog.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,29 @@ Changes in Version 4.6
55
----------------------
66

77
PyMongo 4.6 brings a number of improvements including:
8+
9+
- Added the ``serverMonitoringMode`` URI and keyword argument to :class:`~pymongo.mongo_client.MongoClient`.
10+
- Improved client performance and reduced connection requirements in Function-as-a-service (FaaS)
11+
environments like AWS Lambda, Google Cloud Functions, and Microsoft Azure Functions.
812
- Added the :attr:`pymongo.monitoring.CommandSucceededEvent.database_name` property.
913
- Added the :attr:`pymongo.monitoring.CommandFailedEvent.database_name` property.
14+
- Allow passing a ``dict`` to sort/create_index/hint.
15+
- Added :func:`repr` support to the write result classes:
16+
:class:`~pymongo.results.BulkWriteResult`,
17+
:class:`~pymongo.results.DeleteResult`,
18+
:class:`~pymongo.results.InsertManyResult`,
19+
:class:`~pymongo.results.InsertOneResult`,
20+
:class:`~pymongo.results.UpdateResult`, and
21+
:class:`~pymongo.encryption.RewrapManyDataKeyResult`. For example:
22+
23+
>>> client.t.t.insert_one({})
24+
InsertOneResult(ObjectId('65319acdd55bb3a27ab5502b'), acknowledged=True)
25+
>>> client.t.t.insert_many([{} for _ in range(3)])
26+
InsertManyResult([ObjectId('6532f85e826f2b6125d6ce39'), ObjectId('6532f85e826f2b6125d6ce3a'), ObjectId('6532f85e826f2b6125d6ce3b')], acknowledged=True)
27+
28+
- :meth:`~pymongo.uri_parser.parse_uri` now considers the delimiting slash (``/``)
29+
between hosts and connection options optional. For example,
30+
"mongodb://example.com?tls=true" is now a valid URI.
1031

1132
Changes in Version 4.5
1233
----------------------

pymongo/uri_parser.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,11 @@ def parse_uri(
456456
to their internally-used names. Default: ``True``.
457457
- `connect_timeout` (optional): The maximum time in milliseconds to
458458
wait for a response from the DNS server.
459-
- 'srv_service_name` (optional): A custom SRV service name
459+
- `srv_service_name` (optional): A custom SRV service name
460+
461+
.. versionchanged:: 4.6
462+
The delimiting slash (``/``) between hosts and connection options is now optional.
463+
For example, "mongodb://example.com?tls=true" is now a valid URI.
460464
461465
.. versionchanged:: 4.0
462466
To better follow RFC 3986, unquoted percent signs ("%") are no longer
@@ -506,22 +510,23 @@ def parse_uri(
506510
host_part = path_part
507511
path_part = ""
508512

509-
if not path_part and "?" in host_part:
510-
raise InvalidURI("A '/' is required between the host list and any options.")
511-
512513
if path_part:
513514
dbase, _, opts = path_part.partition("?")
514-
if dbase:
515-
dbase = unquote_plus(dbase)
516-
if "." in dbase:
517-
dbase, collection = dbase.split(".", 1)
518-
if _BAD_DB_CHARS.search(dbase):
519-
raise InvalidURI('Bad database name "%s"' % dbase)
520-
else:
521-
dbase = None
515+
else:
516+
# There was no slash in scheme_free, check for a sole "?".
517+
host_part, _, opts = host_part.partition("?")
518+
519+
if dbase:
520+
dbase = unquote_plus(dbase)
521+
if "." in dbase:
522+
dbase, collection = dbase.split(".", 1)
523+
if _BAD_DB_CHARS.search(dbase):
524+
raise InvalidURI('Bad database name "%s"' % dbase)
525+
else:
526+
dbase = None
522527

523-
if opts:
524-
options.update(split_options(opts, validate, warn, normalize))
528+
if opts:
529+
options.update(split_options(opts, validate, warn, normalize))
525530
if srv_service_name is None:
526531
srv_service_name = options.get("srvServiceName", SRV_SERVICE_NAME)
527532
if "@" in host_part:

test/connection_string/test/invalid-uris.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,6 @@
162162
"auth": null,
163163
"options": null
164164
},
165-
{
166-
"description": "Missing delimiting slash between hosts and options",
167-
"uri": "mongodb://example.com?w=1",
168-
"valid": false,
169-
"warning": null,
170-
"hosts": null,
171-
"auth": null,
172-
"options": null
173-
},
174165
{
175166
"description": "Incomplete key value pair for option",
176167
"uri": "mongodb://example.com/?w",

test/connection_string/test/valid-options.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@
2020
"options": {
2121
"authmechanism": "MONGODB-CR"
2222
}
23+
},
24+
{
25+
"description": "Missing delimiting slash between hosts and options",
26+
"uri": "mongodb://example.com?tls=true",
27+
"valid": true,
28+
"warning": false,
29+
"hosts": [
30+
{
31+
"type": "hostname",
32+
"host": "example.com",
33+
"port": null
34+
}
35+
],
36+
"auth": null,
37+
"options": {
38+
"tls": true
39+
}
2340
}
2441
]
2542
}

0 commit comments

Comments
 (0)