Skip to content

Commit

Permalink
Fixed RootPathSegment GetHashCode implementation. (#6501)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Sep 1, 2023
1 parent 1f9d822 commit 58c9ef1
Show file tree
Hide file tree
Showing 179 changed files with 227 additions and 11 deletions.
16 changes: 7 additions & 9 deletions src/HotChocolate/Core/src/Abstractions/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,24 @@ namespace HotChocolate;
/// </summary>
public abstract class Path : IEquatable<Path>
{
private readonly Path? _parent;

protected Path()
{
Parent = this;
_parent = null;
Length = 0;
}

protected Path(Path parent)
{
Parent = parent ?? throw new ArgumentNullException(nameof(parent));
_parent = parent ?? throw new ArgumentNullException(nameof(parent));
Length = parent.Length + 1;
}

/// <summary>
/// Gets the parent path segment.
/// </summary>
public Path Parent { get; }
public Path Parent => _parent!;

/// <summary>
/// Gets the count of segments this path contains.
Expand Down Expand Up @@ -238,11 +240,7 @@ public override bool Equals(Path? other)

/// <inheritdoc />
public override int GetHashCode()
{
// ReSharper disable NonReadonlyMemberInGetHashCode
return HashCode.Combine(Parent, Length);
// ReSharper restore NonReadonlyMemberInGetHashCode
}
=> 0;

public static RootPathSegment Instance { get; } = new();
}
Expand Down
13 changes: 13 additions & 0 deletions src/HotChocolate/Core/test/Abstractions.Tests/PathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ namespace HotChocolate.Execution.Instrumentation;

public class PathExtensionsTests
{
[Fact]
public void GetHashCode_Test()
{
var path = Path.Root.Append("hero");
Assert.NotEqual(0, path.GetHashCode());
}

[Fact]
public void GetHashCode_Root_Test()
{
Assert.Equal(0, Path.Root.GetHashCode());
}

[Fact]
public void Path_ToString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "abc",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "abc",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "abc",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,7 @@ public void OneOf_A_and_B_Are_Set()
new ObjectFieldNode("b", 123));

// act
void Fail()
=> parser.ParseLiteral(data, oneOfInput, Path.Root.Append("root"));
void Fail() => parser.ParseLiteral(data, oneOfInput, Path.Root.Append("root"));

// assert
Assert.Throws<SerializationException>(Fail).Errors.MatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "root",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "root",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "fieldWithArg",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down Expand Up @@ -66,6 +67,7 @@
"Parent": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down Expand Up @@ -60,6 +61,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "fieldWithArg",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down Expand Up @@ -60,6 +61,7 @@
"Path": {
"Name": "fieldWithArg",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down Expand Up @@ -59,6 +60,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down Expand Up @@ -60,6 +61,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down Expand Up @@ -60,6 +61,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down Expand Up @@ -60,6 +61,7 @@
"Path": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "arguments",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "b",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Path": {
"Name": "b",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Parent": {
"Name": "dog",
"Parent": {
"Parent": null,
"Length": 0,
"IsRoot": true
},
Expand Down
Loading

0 comments on commit 58c9ef1

Please sign in to comment.