BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
2.02k stars 214 forks source link

[BUG] cannot specify mongo ports when connecting to replicaset with connection_string #445

Closed vaizki closed 1 year ago

vaizki commented 1 year ago

Describe the bug If we use init_beanie() to instantiate mongo client with connection_string and connect to a replica set, using port definitions like :27017 breaks the parser.

To Reproduce

% python -m asyncio
asyncio REPL 3.10.2 (main, Mar 15 2022, 15:34:56) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> from beanie import init_beanie
>>> await init_beanie(connection_string='mongodb://192.168.0.1:27017,192.168.0.2:27017/test?replicaSet=test-rs&readPreference=primaryPreferred', document_models=[])          Traceback (most recent call last):
  File "/home/vaizki/dev/admin_api/venv/lib/python3.10/site-packages/yarl/_url.py", line 166, in __new__
    port = val.port
  File "/usr/local/lib/python3.10/urllib/parse.py", line 178, in port
    raise ValueError(message) from None
ValueError: Port could not be cast to integer value as '27017,192.168.0.2:27017'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 446, in result
    return self.__get_result()
  File "/usr/local/lib/python3.10/concurrent/futures/_base.py", line 391, in __get_result
    raise self._exception
  File "<console>", line 1, in <module>
  File "/home/vaizki/dev/admin_api/venv/lib/python3.10/site-packages/beanie/odm/utils/init.py", line 459, in init_beanie
    await Initializer(
  File "/home/vaizki/dev/admin_api/venv/lib/python3.10/site-packages/beanie/odm/utils/init.py", line 67, in __init__
    URL(connection_string).path[1:]
  File "/home/vaizki/dev/admin_api/venv/lib/python3.10/site-packages/yarl/_url.py", line 168, in __new__
    raise ValueError(
ValueError: Invalid URL: port can't be converted to integer

Expected behavior Ports to be parsed host-by-host

vaizki commented 1 year ago

The workaround of course is to create the Motor client beforehand and pass it to init_beanie().

roman-right commented 1 year ago

Hi @vaizki As yarl is not used there more, could you pls try it again?

vaizki commented 1 year ago

@roman-right seems to be ok now, thanks!