Skip to content

Commit 2d99b6b

Browse files
authored
Fix GetHashCode for IEnumerable properties (confluentinc#2432)
* Fix GetHashCode for IEnumerable properties * Minor fix * Minor fix
1 parent 479c6b2 commit 2d99b6b

File tree

7 files changed

+27
-38
lines changed

7 files changed

+27
-38
lines changed

src/Confluent.SchemaRegistry.Encryption/Rest/DataContracts/Kek.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public override int GetHashCode()
9090
var hashCode = (Name != null ? Name.GetHashCode() : 0);
9191
hashCode = (hashCode * 397) ^ (KmsType != null ? KmsType.GetHashCode() : 0);
9292
hashCode = (hashCode * 397) ^ (KmsKeyId != null ? KmsKeyId.GetHashCode() : 0);
93-
hashCode = (hashCode * 397) ^ (KmsProps != null ? KmsProps.GetHashCode() : 0);
93+
hashCode = (hashCode * 397) ^ Utils.IEnumerableHashCode(KmsProps);
9494
hashCode = (hashCode * 397) ^ (Doc != null ? Doc.GetHashCode() : 0);
9595
hashCode = (hashCode * 397) ^ Shared.GetHashCode();
9696
hashCode = (hashCode * 397) ^ Deleted.GetHashCode();

src/Confluent.SchemaRegistry.Encryption/Rest/DataContracts/UpdateKek.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override int GetHashCode()
6060
{
6161
unchecked
6262
{
63-
var hashCode = (KmsProps != null ? KmsProps.GetHashCode() : 0);
63+
var hashCode = Utils.IEnumerableHashCode(KmsProps);
6464
hashCode = (hashCode * 397) ^ (Doc != null ? Doc.GetHashCode() : 0);
6565
hashCode = (hashCode * 397) ^ Shared.GetHashCode();
6666
return hashCode;

src/Confluent.SchemaRegistry/Rest/DataContracts/Metadata.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public override int GetHashCode()
6666
{
6767
unchecked
6868
{
69-
var hashCode = (Tags != null ? Tags.GetHashCode() : 0);
70-
hashCode = (hashCode * 397) ^ (Properties != null ? Properties.GetHashCode() : 0);
71-
hashCode = (hashCode * 397) ^ (Sensitive != null ? Sensitive.GetHashCode() : 0);
69+
var hashCode = Utils.IEnumerableHashCode(Tags);
70+
hashCode = (hashCode * 397) ^ Utils.IEnumerableHashCode(Properties);
71+
hashCode = (hashCode * 397) ^ Utils.IEnumerableHashCode(Sensitive);
7272
return hashCode;
7373
}
7474
}

src/Confluent.SchemaRegistry/Rest/DataContracts/Rule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public override int GetHashCode()
116116
hashCode = (hashCode * 397) ^ (int)Kind;
117117
hashCode = (hashCode * 397) ^ (int)Mode;
118118
hashCode = (hashCode * 397) ^ (Type != null ? Type.GetHashCode() : 0);
119-
hashCode = (hashCode * 397) ^ (Tags != null ? Tags.GetHashCode() : 0);
120-
hashCode = (hashCode * 397) ^ (Params != null ? Params.GetHashCode() : 0);
119+
hashCode = (hashCode * 397) ^ Utils.IEnumerableHashCode(Tags);
120+
hashCode = (hashCode * 397) ^ Utils.IEnumerableHashCode(Params);
121121
hashCode = (hashCode * 397) ^ (Expr != null ? Expr.GetHashCode() : 0);
122122
hashCode = (hashCode * 397) ^ (OnSuccess != null ? OnSuccess.GetHashCode() : 0);
123123
hashCode = (hashCode * 397) ^ (OnFailure != null ? OnFailure.GetHashCode() : 0);

src/Confluent.SchemaRegistry/Rest/DataContracts/RuleSet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public override int GetHashCode()
7777
{
7878
unchecked
7979
{
80-
return ((MigrationRules != null ? MigrationRules.GetHashCode() : 0) * 397) ^
81-
(DomainRules != null ? DomainRules.GetHashCode() : 0);
80+
return (Utils.IEnumerableHashCode(MigrationRules) * 397) ^
81+
Utils.IEnumerableHashCode(DomainRules);
8282
}
8383
}
8484
}

src/Confluent.SchemaRegistry/Rest/DataContracts/Schema.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -271,38 +271,13 @@ public override int GetHashCode()
271271
unchecked
272272
{
273273
var hashCode = SchemaString.GetHashCode();
274-
hashCode = (hashCode * 397) ^ (References != null ? GetListHashCode(References) : 0);
274+
hashCode = (hashCode * 397) ^ Utils.IEnumerableHashCode(References);
275275
hashCode = (hashCode * 397) ^ (Metadata != null ? Metadata.GetHashCode() : 0);
276276
hashCode = (hashCode * 397) ^ (RuleSet != null ? RuleSet.GetHashCode() : 0);
277277
return hashCode;
278278
}
279279
}
280280

281-
/// <summary>
282-
/// Returns a hash code for a list of objects.
283-
/// </summary>
284-
/// <param name="list">
285-
/// The list to get the hash code for.
286-
/// </param>
287-
/// <returns>
288-
/// An integer that specifies a hash value for this instance.
289-
/// </returns>
290-
private int GetListHashCode<T>(IList<T> list)
291-
{
292-
if (list == null || list.Count == 0)
293-
return 0;
294-
295-
unchecked
296-
{
297-
int hash = 0;
298-
foreach (var item in list)
299-
{
300-
hash += item?.GetHashCode() ?? 0;
301-
}
302-
return hash;
303-
}
304-
}
305-
306281
/// <summary>
307282
/// Compares this instance with another instance of this object type and indicates whether
308283
/// this instance precedes, follows, or appears in the same position in the sort order

src/Confluent.SchemaRegistry/Utils.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
//
1515
// Refer to LICENSE for more information.
1616

17-
using System;
18-
using System.Collections;
1917
using System.Collections.Generic;
2018
using System.Linq;
21-
using Confluent.Kafka;
2219

2320
#if NET8_0_OR_GREATER
2421
using System.Buffers.Text;
@@ -74,6 +71,23 @@ public static bool ListEquals<T>(IList<T> a, IList<T> b)
7471
return a.SequenceEqual(b);
7572
}
7673

74+
public static int IEnumerableHashCode<T>(IEnumerable<T> items)
75+
{
76+
if (items == null) return 0;
77+
78+
var hash = 0;
79+
80+
using (var enumerator = items.GetEnumerator())
81+
{
82+
while (enumerator.MoveNext())
83+
{
84+
hash = (hash * 397) ^ (enumerator.Current?.GetHashCode() ?? 0);
85+
}
86+
}
87+
88+
return hash;
89+
}
90+
7791
internal static bool IsBase64String(string value)
7892
{
7993
#if NET8_0_OR_GREATER

0 commit comments

Comments
 (0)