Skip to content

Commit 6e7b652

Browse files
authored
PYTHON-2914 MongoClient should raise an error when given multiple URIs (#747)
1 parent 9cb6477 commit 6e7b652

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ Breaking Changes in 4.0
157157
are passed to the server as-is rather than the previous behavior which
158158
substituted in a projection of ``{"_id": 1}``. This means that an empty
159159
projection will now return the entire document, not just the ``"_id"`` field.
160+
- ``MongoClient()`` now raises a :exc:`~pymongo.errors.ConfigurationError`
161+
when more than one URI is passed into the ``hosts`` argument.
160162

161163

162164
Notable improvements

doc/migrate-to-pymongo4.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ MongoClient cannot execute operations after ``close()``
187187
after being closed. The previous behavior would simply reconnect. However,
188188
now you must create a new instance.
189189

190+
MongoClient raises exception when given more than one URI
191+
.........................................................
192+
193+
``MongoClient()`` now raises a :exc:`~pymongo.errors.ConfigurationError`
194+
when more than one URI is passed into the ``hosts`` argument.
195+
190196
Database
191197
--------
192198

pymongo/mongo_client.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,9 @@ def __init__(
109109
110110
The `host` parameter can be a full `mongodb URI
111111
<http://dochub.mongodb.org/core/connections>`_, in addition to
112-
a simple hostname. It can also be a list of hostnames or
113-
URIs. Any port specified in the host string(s) will override
114-
the `port` parameter. If multiple mongodb URIs containing
115-
database or auth information are passed, the last database,
116-
username, and password present will be used. For username and
112+
a simple hostname. It can also be a list of hostnames but no more
113+
than one URI. Any port specified in the host string(s) will override
114+
the `port` parameter. For username and
117115
passwords reserved characters like ':', '/', '+' and '@' must be
118116
percent encoded following RFC 2396::
119117
@@ -179,8 +177,9 @@ def __init__(
179177
:Parameters:
180178
- `host` (optional): hostname or IP address or Unix domain socket
181179
path of a single mongod or mongos instance to connect to, or a
182-
mongodb URI, or a list of hostnames / mongodb URIs. If `host` is
183-
an IPv6 literal it must be enclosed in '[' and ']' characters
180+
mongodb URI, or a list of hostnames (but no more than one mongodb
181+
URI). If `host` is an IPv6 literal it must be enclosed in '['
182+
and ']' characters
184183
following the RFC2732 URL syntax (e.g. '[::1]' for localhost).
185184
Multihomed and round robin DNS addresses are **not** supported.
186185
- `port` (optional): port number on which to connect
@@ -645,6 +644,9 @@ def __init__(
645644
dbase = None
646645
opts = common._CaseInsensitiveDictionary()
647646
fqdn = None
647+
if len([h for h in host if "/" in h]) > 1:
648+
raise ConfigurationError("host must not contain multiple MongoDB "
649+
"URIs")
648650
for entity in host:
649651
# A hostname can only include a-z, 0-9, '-' and '.'. If we find a '/'
650652
# it must be a URI,

test/test_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ def test_uri_security_options(self):
466466

467467

468468
class TestClient(IntegrationTest):
469+
def test_multiple_uris(self):
470+
with self.assertRaises(ConfigurationError):
471+
MongoClient(host=['mongodb+srv://cluster-a.abc12.mongodb.net',
472+
'mongodb+srv://cluster-b.abc12.mongodb.net',
473+
'mongodb+srv://cluster-c.abc12.mongodb.net'])
469474

470475
def test_max_idle_time_reaper_default(self):
471476
with client_knobs(kill_cursor_frequency=0.1):

0 commit comments

Comments
 (0)