Skip to content

fix(mongodb): connection strings with several hosts work (#1309) - #1325

Merged
ZhuchkaTriplesix merged 1 commit into
devfrom
fix/1309-mongodb-multi-host
Oct 10, 2026
Merged

ZhuchkaTriplesix merged 1 commit into
devfrom
fix/1309-mongodb-multi-host

Conversation

@ZhuchkaTriplesix

Copy link
Copy Markdown
Member

Closes #1309

Problem: mongodb://h1:27017,h2:27017/db?replicaSet=rs0 is valid MongoDB, but Uri.tryParse returns null for it (the port segment is 27017,h2:27017) and Uri.parse throws.

  • New connection from URL answered Invalid URL/URI format.
  • MongoConnection called Uri.parse on the saved string: connecting (_effectiveMongoUri), opening another database (buildUriForDatabase), the effective database name and the SSH tunnel override all threw FormatException. A replica-set string typed into the form's connection-string field could not connect.
  • The certificate paths in such a string were not found (Uri.tryParse null), so TLS files were silently ignored.
  • With mongodb+srv:// and a tunnel, replace(host:, port:) made an SRV URI with a port, which is invalid.

Fix

  • MongoUri (core/database/mongodb_uri.dart): scheme, user info (kept encoded), host list, path, query parameters; databaseName, firstHost, copyWith, forTunnel, toString.
  • MongoConnection uses it for the effective database, the per-database URI (authSource defaulting as before), the TLS parameters and the tunnel target (first seed).
  • forTunnel: one local endpoint plus directConnection=true (unless the string sets it), because the tunnel reaches one server and discovery would otherwise go to the members' own addresses. A mongodb+srv:// string cannot be tunnelled and gets an explanation instead of a malformed URI.
  • The URL import falls back to MongoUri when Uri fails: the first seed fills host / port, the row keeps the whole string. extractSslCertificatePathsFromString reads the query of a string Uri rejects.

Tests: mongodb_uri_test.dart (parse, round trip, SRV, IPv6, tunnel, the connection's database / per-database URI / authSource default / tunnel / SRV refusal, certificate paths) and two URL-import tests.

mongodb://h1:27017,h2:27017/db?replicaSet=rs0 is valid MongoDB, but Dart's
Uri reads the host list as a port: the URL import said Invalid URL/URI
format, and connecting, opening another database and tunnelling threw
FormatException.

- MongoUri splits a MongoDB string without Uri.parse and is used for the
  database name, the per-database URI, the TLS parameters and the tunnel
  target.
- Through an SSH tunnel the string becomes the one local endpoint with
  directConnection=true; a mongodb+srv:// string cannot be tunnelled and
  says so.
- The URL import reads the first seed and keeps the whole string; the
  certificate paths of a host-list string are found.
@github-actions github-actions Bot added bug Something isn't working parser Theme parser epic label: parser connections Database connections, URI parsing, pools P2 Medium priority / Parity & Refactoring labels Oct 10, 2026
@ZhuchkaTriplesix
ZhuchkaTriplesix merged commit e215199 into dev Oct 10, 2026
15 checks passed
@ZhuchkaTriplesix
ZhuchkaTriplesix deleted the fix/1309-mongodb-multi-host branch October 11, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working connections Database connections, URI parsing, pools P2 Medium priority / Parity & Refactoring parser Theme parser epic label: parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant