Skip to content

Commit 0e9b3e7

Browse files
committed
fix #3116 instances of gethashcode in equals implementation (#3117)
* fix #3116 instances of gethashcode in equals implementation * Equals on types and indices now ignores order and implements == operator Conflicts: src/Nest/CommonAbstractions/Infer/RelationName/RelationName.cs src/Tests/ClientConcepts/HighLevel/Inference/TypesAndRelationsInference.doc.cs src/Tests/Framework/Configuration/Versions/ElasticsearchVersion.cs src/Tests/Framework/ElasticsearchVersionTests.cs
1 parent 47d19d8 commit 0e9b3e7

File tree

6 files changed

+41
-21
lines changed

6 files changed

+41
-21
lines changed

src/Nest/CommonAbstractions/Infer/IndexName/IndexName.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ public bool EqualsString(string other)
7272

7373
public bool EqualsMarker(IndexName other)
7474
{
75-
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
75+
if (other == null) return false;
76+
if (!this.Name.IsNullOrEmpty() && !other.Name.IsNullOrEmpty())
7677
return EqualsString(PrefixClusterName(other,other.Name));
7778

78-
if (this.Type != null && other != null && other.Type != null)
79-
return this.GetHashCode() == other.GetHashCode();
80-
return false;
79+
if ((!this.Cluster.IsNullOrEmpty() || !other.Cluster.IsNullOrEmpty()) && this.Cluster != other.Cluster) return false;
80+
81+
return this.Type != null && other?.Type != null && this.Type == other.Type;
8182
}
8283

8384
public string GetString(IConnectionConfigurationValues settings)

src/Nest/CommonAbstractions/Infer/Indices/Indices.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
7474
all => "_all",
7575
many =>
7676
{
77-
var nestSettings = settings as IConnectionSettingsValues;
78-
if (nestSettings == null)
77+
if (!(settings is IConnectionSettingsValues nestSettings))
7978
throw new Exception("Tried to pass index names on querysting but it could not be resolved because no nest settings are available");
8079

8180
var infer = nestSettings.Inferrer;
@@ -85,19 +84,29 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
8584
);
8685
}
8786

87+
public static bool operator ==(Indices left, Indices right) => Equals(left, right);
88+
89+
public static bool operator !=(Indices left, Indices right) => !Equals(left, right);
90+
8891
public override bool Equals(object obj)
8992
{
90-
var other = obj as Indices;
91-
if (other == null) return false;
93+
if (!(obj is Indices other)) return false;
9294
return this.Match(
9395
all => other.Match(a => true, m => false),
9496
many => other.Match(
9597
a => false,
96-
m => this.GetHashCode().Equals(other.GetHashCode())
98+
m => EqualsAllIndices(m.Indices, many.Indices)
9799
)
98100
);
99101
}
100102

103+
private static bool EqualsAllIndices(IReadOnlyList<IndexName> thisIndices, IReadOnlyList<IndexName> otherIndices)
104+
{
105+
if (thisIndices == null && otherIndices == null) return true;
106+
if (thisIndices == null || otherIndices == null) return false;
107+
return thisIndices.Count == otherIndices.Count && !thisIndices.Except(otherIndices).Any();
108+
}
109+
101110
public override int GetHashCode()
102111
{
103112
return this.Match(

src/Nest/CommonAbstractions/Infer/TypeName/TypeName.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public bool EqualsMarker(TypeName other)
7373
{
7474
if (!this.Name.IsNullOrEmpty() && other != null && !other.Name.IsNullOrEmpty())
7575
return EqualsString(other.Name);
76-
if (this.Type != null && other != null && other.Type != null)
77-
return this.GetHashCode() == other.GetHashCode();
76+
if (this.Type != null && other?.Type != null)
77+
return this.Type == other.Type;
7878
return false;
7979
}
8080

src/Nest/CommonAbstractions/Infer/Types/Types.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,20 +85,30 @@ string IUrlParameter.GetString(IConnectionConfigurationValues settings)
8585
);
8686

8787
}
88+
public static bool operator ==(Types left, Types right) => Equals(left, right);
89+
90+
public static bool operator !=(Types left, Types right) => !Equals(left, right);
8891

8992
public override bool Equals(object obj)
9093
{
91-
var other = obj as Types;
92-
if (other == null) return false;
94+
if (!(obj is Types other)) return false;
9395
return this.Match(
9496
all => other.Match(a => true, m => false),
9597
many => other.Match(
9698
a => false,
97-
m => this.GetHashCode().Equals(other.GetHashCode())
99+
m => EqualsAllTypes(m.Types, many.Types)
98100
)
99101
);
100102
}
101103

104+
private static bool EqualsAllTypes(IReadOnlyList<TypeName> thisTypes, IReadOnlyList<TypeName> otherTypes)
105+
{
106+
if (thisTypes == null && otherTypes == null) return true;
107+
if (thisTypes == null || otherTypes == null) return false;
108+
if (thisTypes.Count != otherTypes.Count) return false;
109+
return thisTypes.Count == otherTypes.Count && !thisTypes.Except(otherTypes).Any();
110+
}
111+
102112
public override int GetHashCode()
103113
{
104114
return this.Match(

src/Tests/ClientConcepts/HighLevel/Inference/IndexNameInference.doc.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,22 @@ [U] public void EqualsValidation()
170170
{
171171
var clusterIndex = (IndexName)"cluster_one:p";
172172
var index = (IndexName)"p";
173+
Index<Project>("cluster_one").Should().NotBe(Index<Project>("cluster_two"));
173174

174175
clusterIndex.Should().NotBe(index);
175176
clusterIndex.Should().Be("cluster_one:p");
176177
clusterIndex.Should().Be((IndexName)"cluster_one:p");
177178

178179
Index<Project>().Should().Be(Index<Project>());
179180
Index<Project>().Should().NotBe(Index<Project>("cluster_two"));
180-
Index<Project>("cluster_one").Should().NotBe(Index<Project>("cluster_two"));
181181
Index<Project>("cluster_one").Should().NotBe("cluster_one:project");
182182
Index<Project>().Should().NotBe(Index<Developer>());
183183
Index<Project>("cluster_one").Should().NotBe(Index<Developer>("cluster_one"));
184+
185+
Nest.Indices indices1 = "foo,bar";
186+
Nest.Indices indices2 = "bar,foo";
187+
indices1.Should().Be(indices2);
188+
(indices1 == indices2).Should().BeTrue();
184189
}
185190

186191
//hide

src/Tests/Framework/Configuration/TestConfigurationBase.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using Tests.Framework.Versions;
1+
using Tests.Framework.Versions;
72

83
namespace Tests.Framework.Configuration
94
{

0 commit comments

Comments
 (0)