Skip to content

CSHARP-4413: Equality operator of MongoClientSettings is bugged #976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/MongoDB.Driver/MongoClientSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ public override bool Equals(object obj)
_minConnectionPoolSize == rhs._minConnectionPoolSize &&
object.Equals(_readEncoding, rhs._readEncoding) &&
object.Equals(_readConcern, rhs._readConcern) &&
_readPreference == rhs._readPreference &&
object.Equals(_readPreference, rhs._readPreference) &&
_replicaSetName == rhs._replicaSetName &&
_retryReads == rhs._retryReads &&
_retryWrites == rhs._retryWrites &&
Expand All @@ -1109,7 +1109,7 @@ public override bool Equals(object obj)
_useTls == rhs._useTls &&
_waitQueueSize == rhs._waitQueueSize &&
_waitQueueTimeout == rhs._waitQueueTimeout &&
_writeConcern == rhs._writeConcern &&
object.Equals(_writeConcern, rhs._writeConcern) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better implementing operator ==?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I thought this might be BC, but looks like it's minor. Thoughts @DmitryLukyanov ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it's a BC, but I also think it's minor and I don't see how it can affect users, so I think it's better to do it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think as a general rule Equals should be implemented in terms of Equals.

Sometimes we rely on == being equivalent to Equals but to me it's clearer if Equals is implemented using Equals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DmitryLukyanov we address this in CSHARP-4432

object.Equals(_writeEncoding, rhs._writeEncoding);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/MongoDB.Driver.Tests/MongoClientSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Text;
using System.Threading;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.TestHelpers.XunitExtensions;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Compression;
using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
using MongoDB.Driver.Encryption;
using Moq;
using Xunit;

Expand Down Expand Up @@ -550,6 +554,27 @@ public void TestEquals()
clone.SdamLogFilename = "garbage";
#pragma warning restore CS0618 // Type or member is obsolete
Assert.False(clone.Equals(settings));

// set non default values
settings.AutoEncryptionOptions = new AutoEncryptionOptions(CollectionNamespace.FromFullName("encryption.__keyVault"), new Dictionary<string, IReadOnlyDictionary<string, object>>());
settings.LoggingSettings = new LoggingSettings(null, 123);
settings.ReadConcern = ReadConcern.Majority;
settings.ReadEncoding = new UTF8Encoding(false, false);
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
settings.WriteConcern = WriteConcern.W2;
settings.WriteEncoding = new UTF8Encoding(false, false);

clone = settings.Clone();
clone.AutoEncryptionOptions = clone.AutoEncryptionOptions.With(bypassAutoEncryption: settings.AutoEncryptionOptions.BypassAutoEncryption);
clone.LoggingSettings = new LoggingSettings(null, settings.LoggingSettings.MaxDocumentSize);
clone.ReadConcern = ReadConcern.FromBsonDocument(settings.ReadConcern.ToBsonDocument());
clone.ReadEncoding = new UTF8Encoding(false, false);
clone.ReadPreference = clone.ReadPreference.With(settings.ReadPreference.ReadPreferenceMode);
clone.ServerApi = new ServerApi(settings.ServerApi.Version);
clone.WriteConcern = WriteConcern.FromBsonDocument(settings.WriteConcern.ToBsonDocument());
clone.WriteEncoding = new UTF8Encoding(false, false);

Assert.True(clone.Equals(settings));
}

[Fact]
Expand Down