Skip to content

Commit

Permalink
CSHARP-3315: Implement Equals for serializers.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstam committed Jun 26, 2024
1 parent 17c4ed4 commit 4eaf203
Show file tree
Hide file tree
Showing 240 changed files with 17,366 additions and 470 deletions.
4 changes: 3 additions & 1 deletion src/MongoDB.Bson/MongoDB.Bson.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\MongoDB.Shared\Hasher.cs" Link="Hasher.cs" />
<Compile Include="..\MongoDB.Shared\DictionaryComparer.cs" Link="Shared\DictionaryComparer.cs" />
<Compile Include="..\MongoDB.Shared\Hasher.cs" Link="Shared\Hasher.cs" />
<Compile Include="..\MongoDB.Shared\SequenceComparer.cs" Link="Shared\SequenceComparer.cs" />
</ItemGroup>

</Project>
31 changes: 30 additions & 1 deletion src/MongoDB.Bson/Serialization/BsonClassMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Shared;

namespace MongoDB.Bson.Serialization
{
Expand Down Expand Up @@ -542,6 +542,35 @@ public object CreateInstance()
return creator.Invoke();
}

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
GetType().Equals(obj.GetType()) &&
obj is BsonClassMap other &&
_frozen.Equals(true) && other._frozen.Equals(true) && // BsonClassMaps should only be equal if they are frozen
object.Equals(_baseClassMap, other._baseClassMap) &&
object.Equals(_classType, other._classType) &&
object.Equals(_creator, other._creator) &&
SequenceComparer.Equals(_creatorMaps, other._creatorMaps) &&
SequenceComparer.Equals(_declaredMemberMaps, other._declaredMemberMaps) &&
object.Equals(_discriminator, other._discriminator) &&
_discriminatorIsRequired.Equals(other._discriminatorIsRequired) &&
_extraElementsMemberIndex.Equals(other._extraElementsMemberIndex) &&
object.Equals(_extraElementsMemberMap, other._extraElementsMemberMap) &&
_hasRootClass.Equals(other._hasRootClass) &&
object.Equals(_idMemberMap, other._idMemberMap) &&
_ignoreExtraElements.Equals(other._ignoreExtraElements) &&
_ignoreExtraElementsIsInherited.Equals(other._ignoreExtraElementsIsInherited) &&
_isRootClass.Equals(other._isRootClass) &&
SequenceComparer.Equals(_knownTypes, other._knownTypes);
}

/// <inheritdoc/>
public override int GetHashCode() => 0;

/// <summary>
/// Freezes the class map.
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions src/MongoDB.Bson/Serialization/BsonMemberMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,32 @@ public void Freeze()
_frozen = true;
}

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
GetType().Equals(obj.GetType()) &&
obj is BsonMemberMap other &&
_frozen.Equals(true) && other._frozen.Equals(true) && // BsonMemberMaps should only be equal if they are frozen
object.Equals(_defaultValue, other._defaultValue) &&
object.Equals(_defaultValueCreator, other._defaultValueCreator) &&
_defaultValueSpecified.Equals(other._defaultValueSpecified) &&
object.Equals(_elementName, other._elementName) &&
object.Equals(_idGenerator, other._idGenerator) &&
_ignoreIfDefault.Equals(other._ignoreIfDefault) &&
_ignoreIfNull.Equals(other._ignoreIfNull) &&
_isRequired.Equals(other._isRequired) &&
object.Equals(_memberInfo, other._memberInfo) &&
_order.Equals(other._order) &&
object.Equals(_serializer, other._serializer) &&
object.Equals(_shouldSerializeMethod, other._shouldSerializeMethod);
}

/// <inheritdoc/>
public override int GetHashCode() => 0;

/// <summary>
/// Gets the serializer.
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions src/MongoDB.Bson/Serialization/BsonSerializationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ public object DeserializeValue(BsonValue value)
}
}

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
GetType().Equals(obj.GetType()) &&
obj is BsonSerializationInfo other &&
object.Equals(_elementName, other._elementName) &&
object.Equals(_elementPath, other._elementPath) &&
object.Equals(_nominalType, other._nominalType) &&
object.Equals(_serializer, other._serializer);
}

/// <inheritdoc/>
public override int GetHashCode() => 0;

/// <summary>
/// Merges the new BsonSerializationInfo by taking its properties and concatenating its ElementName.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public string ElementName
}

// public methods
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
GetType().Equals(obj.GetType()) &&
obj is ObjectDiscriminatorConvention other &&
object.Equals(_elementName, other._elementName);
}

/// <summary>
/// Gets the actual type of an object by reading the discriminator from a BsonReader.
/// </summary>
Expand Down Expand Up @@ -143,5 +154,8 @@ public BsonValue GetDiscriminator(Type nominalType, Type actualType)
{
return TypeNameDiscriminator.GetDiscriminator(actualType);
}

/// <inheritdoc/>
public override int GetHashCode() => 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ public string ElementName
}

// public methods
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
GetType().Equals(obj.GetType()) &&
obj is StandardDiscriminatorConvention other &&
object.Equals(_elementName, other._elementName);
}

/// <summary>
/// Gets the actual type of an object by reading the discriminator from a BsonReader.
/// </summary>
Expand Down Expand Up @@ -123,5 +134,8 @@ public Type GetActualType(IBsonReader bsonReader, Type nominalType)
/// <param name="actualType">The actual type.</param>
/// <returns>The discriminator value.</returns>
public abstract BsonValue GetDiscriminator(Type nominalType, Type actualType);

/// <inheritdoc/>
public override int GetHashCode() => 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/

using System;
using MongoDB.Bson.Serialization.Attributes;

namespace MongoDB.Bson.Serialization.Options
{
Expand Down Expand Up @@ -57,6 +56,21 @@ public bool AllowTruncation
}

// public methods
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
GetType().Equals(obj.GetType()) &&
obj is RepresentationConverter other &&
_allowOverflow.Equals(other._allowOverflow) &&
_allowTruncation.Equals(other._allowTruncation);
}

/// <inheritdoc/>
public override int GetHashCode() => 0;

/// <summary>
/// Converts a Decimal128 to a Decimal.
/// </summary>
Expand Down
18 changes: 16 additions & 2 deletions src/MongoDB.Bson/Serialization/Serializers/BitArraySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ private static class Flags

// private fields
private readonly SerializerHelper _helper;
private readonly Int32Serializer _int32Serializer = new Int32Serializer();
private readonly BsonType _representation;

// constructors
Expand Down Expand Up @@ -85,6 +84,21 @@ public BsonType Representation
}

// public methods
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
base.Equals(obj) &&
obj is BitArraySerializer other &&
_representation.Equals(other._representation);
}

/// <inheritdoc/>
public override int GetHashCode() => 0;

// protected methods
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
/// <summary>
/// Deserializes a value.
Expand All @@ -110,7 +124,7 @@ protected override BitArray DeserializeValue(BsonDeserializationContext context,
{
switch (flag)
{
case Flags.Length: length = _int32Serializer.Deserialize(context); break;
case Flags.Length: length = Int32Serializer.Instance.Deserialize(context); break;
case Flags.Bytes: bytes = bsonReader.ReadBytes(); break;
}
});
Expand Down
14 changes: 14 additions & 0 deletions src/MongoDB.Bson/Serialization/Serializers/BooleanSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ public override bool Deserialize(BsonDeserializationContext context, BsonDeseria
}
}

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
base.Equals(obj) &&
obj is BooleanSerializer other &&
_representation.Equals(other._representation);
}

/// <inheritdoc/>
public override int GetHashCode() => 0;

/// <summary>
/// Serializes a value.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/


namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/


namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/


namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,20 @@ public TClass DeserializeClass(BsonDeserializationContext context)
return CreateInstanceUsingCreator(values);
}

/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
base.Equals(obj) &&
obj is BsonClassMapSerializer<TClass> other &&
object.Equals(_classMap, other._classMap);
}

/// <inheritdoc/>
public override int GetHashCode() => 0;

/// <summary>
/// Gets the document Id.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/


namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using MongoDB.Bson.Serialization.Serializers;
using MongoDB.Shared;

namespace MongoDB.Bson.Serialization
{
Expand All @@ -39,6 +40,20 @@ protected BsonDocumentBackedClassSerializer()
}

// public methods
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
base.Equals(obj) &&
obj is BsonDocumentBackedClassSerializer<TClass> other &&
DictionaryComparer.Equals(_memberSerializationInfo, other._memberSerializationInfo);
}

/// <inheritdoc/>
public override int GetHashCode() => 0;

/// <summary>
/// Tries to get the serialization info for a member.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/

using System;
using System.Collections.Generic;
using System.Linq;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization.IdGenerators;

Expand Down Expand Up @@ -47,7 +45,7 @@ public static BsonDocumentSerializer Instance
get { return __instance; }
}

// public methods
// protected methods
/// <summary>
/// Deserializes a value.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/


namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/


namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/


namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/


namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* limitations under the License.
*/


namespace MongoDB.Bson.Serialization.Serializers
{
/// <summary>
Expand Down
Loading

0 comments on commit 4eaf203

Please sign in to comment.