Skip to content

Commit 680cb4e

Browse files
authored
CSHARP-4730: Always encode CompressionType.ZStandard as zstd. (#1207)
1 parent 85e7fab commit 680cb4e

File tree

4 files changed

+40
-32
lines changed

4 files changed

+40
-32
lines changed

src/MongoDB.Driver/MongoClientSettings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Text;
2121
using MongoDB.Bson;
2222
using MongoDB.Driver.Core.Clusters;
23+
using MongoDB.Driver.Core.Compression;
2324
using MongoDB.Driver.Core.Configuration;
2425
using MongoDB.Driver.Core.Misc;
2526
using MongoDB.Driver.Encryption;
@@ -1221,7 +1222,7 @@ public override string ToString()
12211222
}
12221223
if (_compressors?.Any() ?? false)
12231224
{
1224-
sb.AppendFormat("Compressors=[{0}];", string.Join(",", _compressors));
1225+
sb.AppendFormat("Compressors=[{0}];", string.Join(",", _compressors.Select(x => CompressorTypeMapper.ToServerName(x.Type))));
12251226
}
12261227
if (_connectionModeSwitch == ConnectionModeSwitch.UseConnectionMode)
12271228
{

src/MongoDB.Driver/MongoUrlBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ public override string ToString()
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 => CompressorTypeMapper.ToServerName(x.Type))));
927927
foreach (var compressor in _compressors)
928928
{
929929
ParseAndAppendCompressorOptions(query, compressor);

tests/MongoDB.Driver.Tests/MongoClientSettingsTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ public void TestCompressors()
188188
Assert.Throws<InvalidOperationException>(() => { settings.Compressors = compressors; });
189189
}
190190

191+
[Theory]
192+
[InlineData(new[]{CompressorType.Snappy}, "Compressors=[snappy]")]
193+
[InlineData(new[]{CompressorType.Zlib}, "Compressors=[zlib]")]
194+
[InlineData(new[]{CompressorType.ZStandard}, "Compressors=[zstd]")]
195+
[InlineData(new[]{CompressorType.ZStandard, CompressorType.Snappy, CompressorType.Zlib}, "Compressors=[zstd,snappy,zlib]")]
196+
public void Compressors_ToString_outputs_correct_compressors(CompressorType[] compressors, string expectedConnectionString)
197+
{
198+
var settings = new MongoClientSettings {Compressors = compressors.Select(x => new CompressorConfiguration(x)).ToList()};
199+
200+
var result = settings.ToString();
201+
202+
Assert.Contains(expectedConnectionString, result);
203+
}
204+
191205
#pragma warning disable CS0618 // Type or member is obsolete
192206
[Fact]
193207
public void TestConnectionMode()

tests/MongoDB.Driver.Tests/MongoUrlBuilderTests.cs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -346,39 +346,32 @@ public void TestComputedWaitQueueSize_UsingSize()
346346
}
347347

348348
[Theory]
349-
[InlineData(null, null, "mongodb://localhost", null)]
350-
[InlineData(new[] { CompressorType.Zlib }, null, "mongodb://localhost/?compressors={0}", new[] { CompressorType.Zlib })]
351-
[InlineData(new[] { CompressorType.Zlib }, "Level&zlibCompressionLevel&1", "mongodb://localhost/?compressors={0}", new[] { CompressorType.Zlib })]
352-
[InlineData(new[] { CompressorType.Snappy }, null, "mongodb://localhost/?compressors={0}", new[] { CompressorType.Snappy })]
353-
[InlineData(new[] { CompressorType.Snappy, CompressorType.Zlib }, null, "mongodb://localhost/?compressors={0}", new[] { CompressorType.Snappy, CompressorType.Zlib })]
354-
public void TestCompressors(CompressorType[] compressors, string compressionProperty, string formatString, CompressorType[] values)
349+
[InlineData(null, "mongodb://localhost")]
350+
[InlineData(new[] { CompressorType.Zlib }, "mongodb://localhost/?compressors=zlib")]
351+
[InlineData(new[] { CompressorType.ZStandard }, "mongodb://localhost/?compressors=zstd")]
352+
[InlineData(new[] { CompressorType.Snappy }, "mongodb://localhost/?compressors=snappy")]
353+
[InlineData(new[] { CompressorType.Snappy, CompressorType.Zlib }, "mongodb://localhost/?compressors=snappy,zlib")]
354+
[InlineData(new[] { CompressorType.ZStandard, CompressorType.Snappy, CompressorType.Zlib }, "mongodb://localhost/?compressors=zstd,snappy,zlib")]
355+
public void TestCompressors(CompressorType[] compressors, string expectedConnectionString)
355356
{
356357
var subject = new MongoUrlBuilder { Server = _localhost };
357-
if (compressors != null)
358-
{
359-
subject.Compressors = compressors
360-
.Select(x =>
361-
{
362-
var compression = new CompressorConfiguration(x);
363-
if (!string.IsNullOrWhiteSpace(compressionProperty))
364-
{
365-
var @params = compressionProperty.Split('&');
366-
compression.Properties.Add(@params[0], @params[2]);
367-
}
368-
return compression;
369-
})
370-
.ToList();
371-
}
372358

373-
Assert.Equal(compressors ?? new CompressorType[0], subject.Compressors.Select(x => x.Type));
374-
var expectedValues = values == null ? string.Empty : string.Join(",", values.Select(c => c.ToString().ToLower()));
375-
if (!string.IsNullOrWhiteSpace(compressionProperty))
376-
{
377-
var @params = compressionProperty.Split('&');
378-
expectedValues += $"&{@params[1]}={@params[2]}";
379-
}
380-
var canonicalConnectionString = string.Format(formatString, expectedValues);
381-
Assert.Equal(canonicalConnectionString, subject.ToString());
359+
subject.Compressors = compressors?.Select(x => new CompressorConfiguration(x)).ToList();
360+
var connectionString = subject.ToString();
361+
362+
Assert.Equal(expectedConnectionString, connectionString);
363+
}
364+
365+
[Fact]
366+
public void TestCompressorProperties()
367+
{
368+
var subject = new MongoUrlBuilder { Server = _localhost };
369+
370+
var zlibCompressor = new CompressorConfiguration(CompressorType.Zlib) { Properties = {{ "Level", 1 }} };
371+
subject.Compressors = new[] { zlibCompressor };
372+
var connectionString = subject.ToString();
373+
374+
Assert.Equal("mongodb://localhost/?compressors=zlib&zlibCompressionLevel=1", connectionString);
382375
}
383376

384377
[Theory]

0 commit comments

Comments
 (0)