Skip to content

Hasher would not recognize internal references if it encounters the reference before the target node #89

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

Merged
merged 1 commit into from
May 12, 2025
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ and this project adheres _loosely_ to [Semantic Versioning](https://semver.org/s
* During deserialization, `ModelMigrator` would add a feature to the wrong language if the node's concept is from a
different language as the feature.
* `Comparer` didn't recognize internal references to nodes that are children in single containments.
* `Hasher` would not recognize internal references if it encounters the reference before the target node.
### Changed
### Removed
### Deprecated
Expand Down
8 changes: 4 additions & 4 deletions src/LionWeb.Core/Core/Utilities/Hasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void RegisterNode(IReadableNode node)
{
if (_referenceIndices.TryGetValue(node.GetId(), out var index))
{
index.Internal = true;
_referenceIndices[node.GetId()] = index with { Internal = true };
return;
}

Expand All @@ -197,7 +197,7 @@ private int LookupReferenceIndex(IReadableNode node)
{
if (index.Index == _indexUnset)
{
index.Index = ++_nextReferenceIndex;
_referenceIndices[node.GetId()] = index with { Index = ++_nextReferenceIndex };
}

return index.Index;
Expand Down Expand Up @@ -240,9 +240,9 @@ public interface IHash : IEquatable<IHash>;
/// <param name="Hash">Hash code.</param>
public ByteArrayHash(string Algorithm, byte[] Hash)
{
if(Hash.Length < 4)
if (Hash.Length < 4)
throw new ArgumentException("Hash must contain at least 4 bytes.");

this.Algorithm = Algorithm;
this.Hash = Hash;
}
Expand Down
11 changes: 11 additions & 0 deletions test/LionWeb.Core.Test/Utilities/Hasher/HasherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,17 @@ public void Reference_Internal_Equal_After()
Assert.AreEqual(hashA, hashB);
}

[TestMethod]
public void Reference_Internal_Equal_After_OtherId()
{
var targetA = new Line("Ta");
var hashA = new Hasher([new OffsetDuplicate("A") { Source = targetA }, targetA]).Hash();
var targetB = new Line("Tb");
var hashB = new Hasher([new OffsetDuplicate("B") { Source = targetB }, targetB]).Hash();

Assert.AreEqual(hashA, hashB);
}

[TestMethod]
public void Reference_Internal_Different_Before()
{
Expand Down
Loading