Skip to content

Commit fa22217

Browse files
authored
CSHARP-4789: MongoUrlBuilder builds urls with invalid query string fo… (#1179)
1 parent dfc222e commit fa22217

File tree

3 files changed

+85
-86
lines changed

3 files changed

+85
-86
lines changed

src/MongoDB.Driver/MongoUrlBuilder.cs

+42-42
Original file line numberDiff line numberDiff line change
@@ -876,54 +876,54 @@ public override string ToString()
876876
var query = new StringBuilder();
877877
if (_authenticationMechanism != null)
878878
{
879-
query.AppendFormat("authMechanism={0};", _authenticationMechanism);
879+
query.AppendFormat("authMechanism={0}&", _authenticationMechanism);
880880
}
881881
if (_authenticationMechanismProperties.Any())
882882
{
883883
query.AppendFormat(
884-
"authMechanismProperties={0};",
884+
"authMechanismProperties={0}&",
885885
string.Join(",", _authenticationMechanismProperties
886886
.Select(x => string.Format("{0}:{1}", x.Key, x.Value)).ToArray()));
887887
}
888888
if (_authenticationSource != null)
889889
{
890-
query.AppendFormat("authSource={0};", _authenticationSource);
890+
query.AppendFormat("authSource={0}&", _authenticationSource);
891891
}
892892
if (_applicationName != null)
893893
{
894-
query.AppendFormat("appname={0};", _applicationName);
894+
query.AppendFormat("appname={0}&", _applicationName);
895895
}
896896
if (_ipv6)
897897
{
898-
query.AppendFormat("ipv6=true;");
898+
query.AppendFormat("ipv6=true&");
899899
}
900900
if (_scheme == ConnectionStringScheme.MongoDBPlusSrv)
901901
{
902902
if (!_useTls)
903903
{
904-
query.AppendFormat("tls=false;");
904+
query.AppendFormat("tls=false&");
905905
}
906906
}
907907
else
908908
{
909909
if (_useTls)
910910
{
911-
query.AppendFormat("tls=true;");
911+
query.AppendFormat("tls=true&");
912912
}
913913
}
914914
if (_allowInsecureTls)
915915
{
916-
query.AppendFormat("tlsInsecure=true;");
916+
query.AppendFormat("tlsInsecure=true&");
917917
}
918918

919919
if (_tlsDisableCertificateRevocationCheck != null)
920920
{
921-
query.AppendFormat("tlsDisableCertificateRevocationCheck={0};", JsonConvert.ToString(_tlsDisableCertificateRevocationCheck.Value));
921+
query.AppendFormat("tlsDisableCertificateRevocationCheck={0}&", JsonConvert.ToString(_tlsDisableCertificateRevocationCheck.Value));
922922
}
923923

924924
if (_compressors?.Any() ?? false)
925925
{
926-
query.AppendFormat("compressors={0};", string.Join(",", _compressors.Select(x => x.Type.ToString().ToLowerInvariant())));
926+
query.AppendFormat("compressors={0}&", string.Join(",", _compressors.Select(x => x.Type.ToString().ToLowerInvariant())));
927927
foreach (var compressor in _compressors)
928928
{
929929
ParseAndAppendCompressorOptions(query, compressor);
@@ -934,142 +934,142 @@ public override string ToString()
934934
{
935935
if (_connectionMode != ConnectionMode.Automatic)
936936
{
937-
query.AppendFormat("connect={0};", MongoUtils.ToCamelCase(_connectionMode.ToString()));
937+
query.AppendFormat("connect={0}&", MongoUtils.ToCamelCase(_connectionMode.ToString()));
938938
}
939939
}
940940
else if (_connectionModeSwitch == ConnectionModeSwitch.UseDirectConnection)
941941
#pragma warning restore CS0618
942942
{
943943
if (_directConnection.HasValue)
944944
{
945-
query.AppendFormat("directConnection={0};", JsonConvert.ToString(_directConnection.Value));
945+
query.AppendFormat("directConnection={0}&", JsonConvert.ToString(_directConnection.Value));
946946
}
947947
}
948948
if (!string.IsNullOrEmpty(_replicaSetName))
949949
{
950-
query.AppendFormat("replicaSet={0};", _replicaSetName);
950+
query.AppendFormat("replicaSet={0}&", _replicaSetName);
951951
}
952952
if (_readConcernLevel != null)
953953
{
954-
query.AppendFormat("readConcernLevel={0};", MongoUtils.ToCamelCase(_readConcernLevel.Value.ToString()));
954+
query.AppendFormat("readConcernLevel={0}&", MongoUtils.ToCamelCase(_readConcernLevel.Value.ToString()));
955955
}
956956
if (_readPreference != null)
957957
{
958-
query.AppendFormat("readPreference={0};", MongoUtils.ToCamelCase(_readPreference.ReadPreferenceMode.ToString()));
958+
query.AppendFormat("readPreference={0}&", MongoUtils.ToCamelCase(_readPreference.ReadPreferenceMode.ToString()));
959959
if (_readPreference.TagSets != null)
960960
{
961961
foreach (var tagSet in _readPreference.TagSets)
962962
{
963-
query.AppendFormat("readPreferenceTags={0};", string.Join(",", tagSet.Tags.Select(t => string.Format("{0}:{1}", t.Name, t.Value)).ToArray()));
963+
query.AppendFormat("readPreferenceTags={0}&", string.Join(",", tagSet.Tags.Select(t => string.Format("{0}:{1}", t.Name, t.Value)).ToArray()));
964964
}
965965
}
966966
if (_readPreference.MaxStaleness.HasValue)
967967
{
968-
query.AppendFormat("maxStaleness={0};", FormatTimeSpan(_readPreference.MaxStaleness.Value));
968+
query.AppendFormat("maxStaleness={0}&", FormatTimeSpan(_readPreference.MaxStaleness.Value));
969969
}
970970
}
971971
if (_fsync != null)
972972
{
973-
query.AppendFormat("fsync={0};", JsonConvert.ToString(_fsync.Value));
973+
query.AppendFormat("fsync={0}&", JsonConvert.ToString(_fsync.Value));
974974
}
975975
if (_journal != null)
976976
{
977-
query.AppendFormat("journal={0};", JsonConvert.ToString(_journal.Value));
977+
query.AppendFormat("journal={0}&", JsonConvert.ToString(_journal.Value));
978978
}
979979
if (_w != null)
980980
{
981-
query.AppendFormat("w={0};", _w);
981+
query.AppendFormat("w={0}&", _w);
982982
}
983983
if (_wTimeout != null)
984984
{
985-
query.AppendFormat("wtimeout={0};", FormatTimeSpan(_wTimeout.Value));
985+
query.AppendFormat("wtimeout={0}&", FormatTimeSpan(_wTimeout.Value));
986986
}
987987
if (_connectTimeout != MongoDefaults.ConnectTimeout)
988988
{
989-
query.AppendFormat("connectTimeout={0};", FormatTimeSpan(_connectTimeout));
989+
query.AppendFormat("connectTimeout={0}&", FormatTimeSpan(_connectTimeout));
990990
}
991991
if (_heartbeatInterval != ServerSettings.DefaultHeartbeatInterval)
992992
{
993-
query.AppendFormat("heartbeatInterval={0};", FormatTimeSpan(_heartbeatInterval));
993+
query.AppendFormat("heartbeatInterval={0}&", FormatTimeSpan(_heartbeatInterval));
994994
}
995995
if (_heartbeatTimeout != ServerSettings.DefaultHeartbeatTimeout)
996996
{
997-
query.AppendFormat("heartbeatTimeout={0};", FormatTimeSpan(_heartbeatTimeout));
997+
query.AppendFormat("heartbeatTimeout={0}&", FormatTimeSpan(_heartbeatTimeout));
998998
}
999999
if (_loadBalanced)
10001000
{
1001-
query.AppendFormat("loadBalanced={0};", JsonConvert.ToString(_loadBalanced));
1001+
query.AppendFormat("loadBalanced={0}&", JsonConvert.ToString(_loadBalanced));
10021002
}
10031003
if (_localThreshold != MongoDefaults.LocalThreshold)
10041004
{
1005-
query.AppendFormat("localThreshold={0};", FormatTimeSpan(_localThreshold));
1005+
query.AppendFormat("localThreshold={0}&", FormatTimeSpan(_localThreshold));
10061006
}
10071007
if (_maxConnecting != MongoInternalDefaults.ConnectionPool.MaxConnecting)
10081008
{
1009-
query.AppendFormat("maxConnecting={0};", _maxConnecting);
1009+
query.AppendFormat("maxConnecting={0}&", _maxConnecting);
10101010
}
10111011
if (_maxConnectionIdleTime != MongoDefaults.MaxConnectionIdleTime)
10121012
{
1013-
query.AppendFormat("maxIdleTime={0};", FormatTimeSpan(_maxConnectionIdleTime));
1013+
query.AppendFormat("maxIdleTime={0}&", FormatTimeSpan(_maxConnectionIdleTime));
10141014
}
10151015
if (_maxConnectionLifeTime != MongoDefaults.MaxConnectionLifeTime)
10161016
{
1017-
query.AppendFormat("maxLifeTime={0};", FormatTimeSpan(_maxConnectionLifeTime));
1017+
query.AppendFormat("maxLifeTime={0}&", FormatTimeSpan(_maxConnectionLifeTime));
10181018
}
10191019
if (_maxConnectionPoolSize != MongoDefaults.MaxConnectionPoolSize)
10201020
{
1021-
query.AppendFormat("maxPoolSize={0};", _maxConnectionPoolSize);
1021+
query.AppendFormat("maxPoolSize={0}&", _maxConnectionPoolSize);
10221022
}
10231023
if (_minConnectionPoolSize != MongoDefaults.MinConnectionPoolSize)
10241024
{
1025-
query.AppendFormat("minPoolSize={0};", _minConnectionPoolSize);
1025+
query.AppendFormat("minPoolSize={0}&", _minConnectionPoolSize);
10261026
}
10271027
if (_serverSelectionTimeout != MongoDefaults.ServerSelectionTimeout)
10281028
{
1029-
query.AppendFormat("serverSelectionTimeout={0};", FormatTimeSpan(_serverSelectionTimeout));
1029+
query.AppendFormat("serverSelectionTimeout={0}&", FormatTimeSpan(_serverSelectionTimeout));
10301030
}
10311031
if (_socketTimeout != MongoDefaults.SocketTimeout)
10321032
{
1033-
query.AppendFormat("socketTimeout={0};", FormatTimeSpan(_socketTimeout));
1033+
query.AppendFormat("socketTimeout={0}&", FormatTimeSpan(_socketTimeout));
10341034
}
10351035
#pragma warning disable 618
10361036
if (_waitQueueMultiple != 0.0 && _waitQueueMultiple != MongoDefaults.WaitQueueMultiple)
10371037
#pragma warning restore 618
10381038
{
1039-
query.AppendFormat("waitQueueMultiple={0};", _waitQueueMultiple);
1039+
query.AppendFormat("waitQueueMultiple={0}&", _waitQueueMultiple);
10401040
}
10411041
#pragma warning disable 618
10421042
if (_waitQueueSize != 0 && _waitQueueSize != MongoDefaults.WaitQueueSize)
10431043
#pragma warning restore 618
10441044
{
1045-
query.AppendFormat("waitQueueSize={0};", _waitQueueSize);
1045+
query.AppendFormat("waitQueueSize={0}&", _waitQueueSize);
10461046
}
10471047
if (_waitQueueTimeout != MongoDefaults.WaitQueueTimeout)
10481048
{
1049-
query.AppendFormat("waitQueueTimeout={0};", FormatTimeSpan(WaitQueueTimeout));
1049+
query.AppendFormat("waitQueueTimeout={0}&", FormatTimeSpan(WaitQueueTimeout));
10501050
}
10511051
#pragma warning disable 618
10521052
var defaultGuidRepresentation = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 ? BsonDefaults.GuidRepresentation : GuidRepresentation.Unspecified;
10531053
if (_guidRepresentation != defaultGuidRepresentation)
10541054
{
1055-
query.AppendFormat("uuidRepresentation={0};", (_guidRepresentation == GuidRepresentation.CSharpLegacy) ? "csharpLegacy" : MongoUtils.ToCamelCase(_guidRepresentation.ToString()));
1055+
query.AppendFormat("uuidRepresentation={0}&", (_guidRepresentation == GuidRepresentation.CSharpLegacy) ? "csharpLegacy" : MongoUtils.ToCamelCase(_guidRepresentation.ToString()));
10561056
}
10571057
#pragma warning restore 618
10581058
if (!_retryReads.GetValueOrDefault(true))
10591059
{
1060-
query.AppendFormat("retryReads=false;");
1060+
query.AppendFormat("retryReads=false&");
10611061
}
10621062
if (_retryWrites.HasValue)
10631063
{
1064-
query.AppendFormat("retryWrites={0};", JsonConvert.ToString(_retryWrites.Value));
1064+
query.AppendFormat("retryWrites={0}&", JsonConvert.ToString(_retryWrites.Value));
10651065
}
10661066
if (_srvMaxHosts.HasValue)
10671067
{
1068-
query.AppendFormat("srvMaxHosts={0};", _srvMaxHosts);
1068+
query.AppendFormat("srvMaxHosts={0}&", _srvMaxHosts);
10691069
}
10701070
if (query.Length != 0)
10711071
{
1072-
query.Length = query.Length - 1; // remove trailing ";"
1072+
query.Length = query.Length - 1; // remove trailing "&"
10731073
if (_databaseName == null)
10741074
{
10751075
url.Append("/");
@@ -1228,7 +1228,7 @@ private static void ParseAndAppendCompressorOptions(StringBuilder builder, Compr
12281228
{
12291229
if (compressorConfiguration.Properties.TryGetValue("Level", out var zlibCompressionLevel))
12301230
{
1231-
builder.AppendFormat("zlibCompressionLevel={0};", zlibCompressionLevel);
1231+
builder.AppendFormat("zlibCompressionLevel={0}&", zlibCompressionLevel);
12321232
}
12331233
}
12341234
break;

0 commit comments

Comments
 (0)