Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed RootPathSegment GetHashCode implementation. #6501

Merged
merged 4 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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