From c1c6fb1be9e3d5866b494c6bedbd6f84dbf2b7e7 Mon Sep 17 00:00:00 2001 From: Niko Stotz Date: Mon, 12 May 2025 12:58:40 +0200 Subject: [PATCH] Hasher would not recognize internal references if it encounters the reference before the target node --- CHANGELOG.md | 1 + src/LionWeb.Core/Core/Utilities/Hasher.cs | 8 ++++---- .../LionWeb.Core.Test/Utilities/Hasher/HasherTests.cs | 11 +++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 370dc984..098dcfc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/LionWeb.Core/Core/Utilities/Hasher.cs b/src/LionWeb.Core/Core/Utilities/Hasher.cs index 8bd6fef7..788174c7 100644 --- a/src/LionWeb.Core/Core/Utilities/Hasher.cs +++ b/src/LionWeb.Core/Core/Utilities/Hasher.cs @@ -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; } @@ -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; @@ -240,9 +240,9 @@ public interface IHash : IEquatable; /// Hash code. 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; } diff --git a/test/LionWeb.Core.Test/Utilities/Hasher/HasherTests.cs b/test/LionWeb.Core.Test/Utilities/Hasher/HasherTests.cs index 61593ce5..7b4f50c8 100644 --- a/test/LionWeb.Core.Test/Utilities/Hasher/HasherTests.cs +++ b/test/LionWeb.Core.Test/Utilities/Hasher/HasherTests.cs @@ -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() {