Skip to content

Commit 4eaf203

Browse files
committed
CSHARP-3315: Implement Equals for serializers.
1 parent 17c4ed4 commit 4eaf203

File tree

240 files changed

+17366
-470
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

240 files changed

+17366
-470
lines changed

src/MongoDB.Bson/MongoDB.Bson.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
</ItemGroup>
2121

2222
<ItemGroup>
23-
<Compile Include="..\MongoDB.Shared\Hasher.cs" Link="Hasher.cs" />
23+
<Compile Include="..\MongoDB.Shared\DictionaryComparer.cs" Link="Shared\DictionaryComparer.cs" />
24+
<Compile Include="..\MongoDB.Shared\Hasher.cs" Link="Shared\Hasher.cs" />
25+
<Compile Include="..\MongoDB.Shared\SequenceComparer.cs" Link="Shared\SequenceComparer.cs" />
2426
</ItemGroup>
2527

2628
</Project>

src/MongoDB.Bson/Serialization/BsonClassMap.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
using System.Linq;
2020
using System.Linq.Expressions;
2121
using System.Reflection;
22-
using System.Runtime.CompilerServices;
2322
using System.Runtime.Serialization;
2423
using MongoDB.Bson.IO;
2524
using MongoDB.Bson.Serialization.Conventions;
25+
using MongoDB.Shared;
2626

2727
namespace MongoDB.Bson.Serialization
2828
{
@@ -542,6 +542,35 @@ public object CreateInstance()
542542
return creator.Invoke();
543543
}
544544

545+
/// <inheritdoc/>
546+
public override bool Equals(object obj)
547+
{
548+
if (object.ReferenceEquals(obj, null)) { return false; }
549+
if (object.ReferenceEquals(this, obj)) { return true; }
550+
return
551+
GetType().Equals(obj.GetType()) &&
552+
obj is BsonClassMap other &&
553+
_frozen.Equals(true) && other._frozen.Equals(true) && // BsonClassMaps should only be equal if they are frozen
554+
object.Equals(_baseClassMap, other._baseClassMap) &&
555+
object.Equals(_classType, other._classType) &&
556+
object.Equals(_creator, other._creator) &&
557+
SequenceComparer.Equals(_creatorMaps, other._creatorMaps) &&
558+
SequenceComparer.Equals(_declaredMemberMaps, other._declaredMemberMaps) &&
559+
object.Equals(_discriminator, other._discriminator) &&
560+
_discriminatorIsRequired.Equals(other._discriminatorIsRequired) &&
561+
_extraElementsMemberIndex.Equals(other._extraElementsMemberIndex) &&
562+
object.Equals(_extraElementsMemberMap, other._extraElementsMemberMap) &&
563+
_hasRootClass.Equals(other._hasRootClass) &&
564+
object.Equals(_idMemberMap, other._idMemberMap) &&
565+
_ignoreExtraElements.Equals(other._ignoreExtraElements) &&
566+
_ignoreExtraElementsIsInherited.Equals(other._ignoreExtraElementsIsInherited) &&
567+
_isRootClass.Equals(other._isRootClass) &&
568+
SequenceComparer.Equals(_knownTypes, other._knownTypes);
569+
}
570+
571+
/// <inheritdoc/>
572+
public override int GetHashCode() => 0;
573+
545574
/// <summary>
546575
/// Freezes the class map.
547576
/// </summary>

src/MongoDB.Bson/Serialization/BsonMemberMap.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,32 @@ public void Freeze()
264264
_frozen = true;
265265
}
266266

267+
/// <inheritdoc/>
268+
public override bool Equals(object obj)
269+
{
270+
if (object.ReferenceEquals(obj, null)) { return false; }
271+
if (object.ReferenceEquals(this, obj)) { return true; }
272+
return
273+
GetType().Equals(obj.GetType()) &&
274+
obj is BsonMemberMap other &&
275+
_frozen.Equals(true) && other._frozen.Equals(true) && // BsonMemberMaps should only be equal if they are frozen
276+
object.Equals(_defaultValue, other._defaultValue) &&
277+
object.Equals(_defaultValueCreator, other._defaultValueCreator) &&
278+
_defaultValueSpecified.Equals(other._defaultValueSpecified) &&
279+
object.Equals(_elementName, other._elementName) &&
280+
object.Equals(_idGenerator, other._idGenerator) &&
281+
_ignoreIfDefault.Equals(other._ignoreIfDefault) &&
282+
_ignoreIfNull.Equals(other._ignoreIfNull) &&
283+
_isRequired.Equals(other._isRequired) &&
284+
object.Equals(_memberInfo, other._memberInfo) &&
285+
_order.Equals(other._order) &&
286+
object.Equals(_serializer, other._serializer) &&
287+
object.Equals(_shouldSerializeMethod, other._shouldSerializeMethod);
288+
}
289+
290+
/// <inheritdoc/>
291+
public override int GetHashCode() => 0;
292+
267293
/// <summary>
268294
/// Gets the serializer.
269295
/// </summary>

src/MongoDB.Bson/Serialization/BsonSerializationInfo.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,23 @@ public object DeserializeValue(BsonValue value)
127127
}
128128
}
129129

130+
/// <inheritdoc/>
131+
public override bool Equals(object obj)
132+
{
133+
if (object.ReferenceEquals(obj, null)) { return false; }
134+
if (object.ReferenceEquals(this, obj)) { return true; }
135+
return
136+
GetType().Equals(obj.GetType()) &&
137+
obj is BsonSerializationInfo other &&
138+
object.Equals(_elementName, other._elementName) &&
139+
object.Equals(_elementPath, other._elementPath) &&
140+
object.Equals(_nominalType, other._nominalType) &&
141+
object.Equals(_serializer, other._serializer);
142+
}
143+
144+
/// <inheritdoc/>
145+
public override int GetHashCode() => 0;
146+
130147
/// <summary>
131148
/// Merges the new BsonSerializationInfo by taking its properties and concatenating its ElementName.
132149
/// </summary>

src/MongoDB.Bson/Serialization/Conventions/ObjectDiscriminatorConvention.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ public string ElementName
6969
}
7070

7171
// public methods
72+
/// <inheritdoc/>
73+
public override bool Equals(object obj)
74+
{
75+
if (object.ReferenceEquals(obj, null)) { return false; }
76+
if (object.ReferenceEquals(this, obj)) { return true; }
77+
return
78+
GetType().Equals(obj.GetType()) &&
79+
obj is ObjectDiscriminatorConvention other &&
80+
object.Equals(_elementName, other._elementName);
81+
}
82+
7283
/// <summary>
7384
/// Gets the actual type of an object by reading the discriminator from a BsonReader.
7485
/// </summary>
@@ -143,5 +154,8 @@ public BsonValue GetDiscriminator(Type nominalType, Type actualType)
143154
{
144155
return TypeNameDiscriminator.GetDiscriminator(actualType);
145156
}
157+
158+
/// <inheritdoc/>
159+
public override int GetHashCode() => 0;
146160
}
147161
}

src/MongoDB.Bson/Serialization/Conventions/StandardDiscriminatorConvention.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ public string ElementName
7777
}
7878

7979
// public methods
80+
/// <inheritdoc/>
81+
public override bool Equals(object obj)
82+
{
83+
if (object.ReferenceEquals(obj, null)) { return false; }
84+
if (object.ReferenceEquals(this, obj)) { return true; }
85+
return
86+
GetType().Equals(obj.GetType()) &&
87+
obj is StandardDiscriminatorConvention other &&
88+
object.Equals(_elementName, other._elementName);
89+
}
90+
8091
/// <summary>
8192
/// Gets the actual type of an object by reading the discriminator from a BsonReader.
8293
/// </summary>
@@ -123,5 +134,8 @@ public Type GetActualType(IBsonReader bsonReader, Type nominalType)
123134
/// <param name="actualType">The actual type.</param>
124135
/// <returns>The discriminator value.</returns>
125136
public abstract BsonValue GetDiscriminator(Type nominalType, Type actualType);
137+
138+
/// <inheritdoc/>
139+
public override int GetHashCode() => 0;
126140
}
127141
}

src/MongoDB.Bson/Serialization/Options/RepresentationConverter.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515

1616
using System;
17-
using MongoDB.Bson.Serialization.Attributes;
1817

1918
namespace MongoDB.Bson.Serialization.Options
2019
{
@@ -57,6 +56,21 @@ public bool AllowTruncation
5756
}
5857

5958
// public methods
59+
/// <inheritdoc/>
60+
public override bool Equals(object obj)
61+
{
62+
if (object.ReferenceEquals(obj, null)) { return false; }
63+
if (object.ReferenceEquals(this, obj)) { return true; }
64+
return
65+
GetType().Equals(obj.GetType()) &&
66+
obj is RepresentationConverter other &&
67+
_allowOverflow.Equals(other._allowOverflow) &&
68+
_allowTruncation.Equals(other._allowTruncation);
69+
}
70+
71+
/// <inheritdoc/>
72+
public override int GetHashCode() => 0;
73+
6074
/// <summary>
6175
/// Converts a Decimal128 to a Decimal.
6276
/// </summary>

src/MongoDB.Bson/Serialization/Serializers/BitArraySerializer.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ private static class Flags
3434

3535
// private fields
3636
private readonly SerializerHelper _helper;
37-
private readonly Int32Serializer _int32Serializer = new Int32Serializer();
3837
private readonly BsonType _representation;
3938

4039
// constructors
@@ -85,6 +84,21 @@ public BsonType Representation
8584
}
8685

8786
// public methods
87+
/// <inheritdoc/>
88+
public override bool Equals(object obj)
89+
{
90+
if (object.ReferenceEquals(obj, null)) { return false; }
91+
if (object.ReferenceEquals(this, obj)) { return true; }
92+
return
93+
base.Equals(obj) &&
94+
obj is BitArraySerializer other &&
95+
_representation.Equals(other._representation);
96+
}
97+
98+
/// <inheritdoc/>
99+
public override int GetHashCode() => 0;
100+
101+
// protected methods
88102
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
89103
/// <summary>
90104
/// Deserializes a value.
@@ -110,7 +124,7 @@ protected override BitArray DeserializeValue(BsonDeserializationContext context,
110124
{
111125
switch (flag)
112126
{
113-
case Flags.Length: length = _int32Serializer.Deserialize(context); break;
127+
case Flags.Length: length = Int32Serializer.Instance.Deserialize(context); break;
114128
case Flags.Bytes: bytes = bsonReader.ReadBytes(); break;
115129
}
116130
});

src/MongoDB.Bson/Serialization/Serializers/BooleanSerializer.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ public override bool Deserialize(BsonDeserializationContext context, BsonDeseria
121121
}
122122
}
123123

124+
/// <inheritdoc/>
125+
public override bool Equals(object obj)
126+
{
127+
if (object.ReferenceEquals(obj, null)) { return false; }
128+
if (object.ReferenceEquals(this, obj)) { return true; }
129+
return
130+
base.Equals(obj) &&
131+
obj is BooleanSerializer other &&
132+
_representation.Equals(other._representation);
133+
}
134+
135+
/// <inheritdoc/>
136+
public override int GetHashCode() => 0;
137+
124138
/// <summary>
125139
/// Serializes a value.
126140
/// </summary>

src/MongoDB.Bson/Serialization/Serializers/BsonArraySerializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
1716
namespace MongoDB.Bson.Serialization.Serializers
1817
{
1918
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonBinaryDataSerializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
1716
namespace MongoDB.Bson.Serialization.Serializers
1817
{
1918
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonBooleanSerializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
1716
namespace MongoDB.Bson.Serialization.Serializers
1817
{
1918
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ public TClass DeserializeClass(BsonDeserializationContext context)
295295
return CreateInstanceUsingCreator(values);
296296
}
297297

298+
/// <inheritdoc/>
299+
public override bool Equals(object obj)
300+
{
301+
if (object.ReferenceEquals(obj, null)) { return false; }
302+
if (object.ReferenceEquals(this, obj)) { return true; }
303+
return
304+
base.Equals(obj) &&
305+
obj is BsonClassMapSerializer<TClass> other &&
306+
object.Equals(_classMap, other._classMap);
307+
}
308+
309+
/// <inheritdoc/>
310+
public override int GetHashCode() => 0;
311+
298312
/// <summary>
299313
/// Gets the document Id.
300314
/// </summary>

src/MongoDB.Bson/Serialization/Serializers/BsonDateTimeSerializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
1716
namespace MongoDB.Bson.Serialization.Serializers
1817
{
1918
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonDocumentBackedClassSerializer.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using MongoDB.Bson.Serialization.Serializers;
19+
using MongoDB.Shared;
1920

2021
namespace MongoDB.Bson.Serialization
2122
{
@@ -39,6 +40,20 @@ protected BsonDocumentBackedClassSerializer()
3940
}
4041

4142
// public methods
43+
/// <inheritdoc/>
44+
public override bool Equals(object obj)
45+
{
46+
if (object.ReferenceEquals(obj, null)) { return false; }
47+
if (object.ReferenceEquals(this, obj)) { return true; }
48+
return
49+
base.Equals(obj) &&
50+
obj is BsonDocumentBackedClassSerializer<TClass> other &&
51+
DictionaryComparer.Equals(_memberSerializationInfo, other._memberSerializationInfo);
52+
}
53+
54+
/// <inheritdoc/>
55+
public override int GetHashCode() => 0;
56+
4257
/// <summary>
4358
/// Tries to get the serialization info for a member.
4459
/// </summary>

src/MongoDB.Bson/Serialization/Serializers/BsonDocumentSerializer.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
*/
1515

1616
using System;
17-
using System.Collections.Generic;
18-
using System.Linq;
1917
using MongoDB.Bson.IO;
2018
using MongoDB.Bson.Serialization.IdGenerators;
2119

@@ -47,7 +45,7 @@ public static BsonDocumentSerializer Instance
4745
get { return __instance; }
4846
}
4947

50-
// public methods
48+
// protected methods
5149
/// <summary>
5250
/// Deserializes a value.
5351
/// </summary>

src/MongoDB.Bson/Serialization/Serializers/BsonDoubleSerializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
1716
namespace MongoDB.Bson.Serialization.Serializers
1817
{
1918
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonInt32Serializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
1716
namespace MongoDB.Bson.Serialization.Serializers
1817
{
1918
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonInt64Serializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
1716
namespace MongoDB.Bson.Serialization.Serializers
1817
{
1918
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonJavaScriptSerializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
1716
namespace MongoDB.Bson.Serialization.Serializers
1817
{
1918
/// <summary>

src/MongoDB.Bson/Serialization/Serializers/BsonJavaScriptWithScopeSerializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
1716
namespace MongoDB.Bson.Serialization.Serializers
1817
{
1918
/// <summary>

0 commit comments

Comments
 (0)