From bd53aa734e787849e69f8ae0ad29a68c6bcf7cd7 Mon Sep 17 00:00:00 2001 From: Gustas <37534529+PunkPun@users.noreply.github.com> Date: Wed, 20 Aug 2025 13:38:09 +0300 Subject: [PATCH] Handle comments when merging yaml --- OpenRA.Game/MiniYaml.cs | 9 +++++++ OpenRA.Test/OpenRA.Game/MiniYamlTest.cs | 33 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/OpenRA.Game/MiniYaml.cs b/OpenRA.Game/MiniYaml.cs index b1934e1ee0..d84fea5c10 100644 --- a/OpenRA.Game/MiniYaml.cs +++ b/OpenRA.Game/MiniYaml.cs @@ -506,6 +506,9 @@ namespace OpenRA var ret = new List(existingNodes.Count); foreach (var n in existingNodes) { + if (n.Key == null) + continue; + if (keys.Add(n.Key)) ret.Add(n); else @@ -529,6 +532,9 @@ namespace OpenRA for (var i = 0; i < nodes.Count; i++) { var node = nodes[i]; + if (node.Key == null) + continue; + if (node.Key.StartsWith('-')) { if (ret == null) @@ -599,6 +605,9 @@ namespace OpenRA void MergeNode(MiniYamlNode node) { + if (node.Key == null) + return; + // Append Removal nodes to the result. // Therefore: we know the remainder of the method deals with a plain node. if (node.Key.StartsWith('-')) diff --git a/OpenRA.Test/OpenRA.Game/MiniYamlTest.cs b/OpenRA.Test/OpenRA.Game/MiniYamlTest.cs index c7d375b6a6..303904efd6 100644 --- a/OpenRA.Test/OpenRA.Game/MiniYamlTest.cs +++ b/OpenRA.Test/OpenRA.Game/MiniYamlTest.cs @@ -859,6 +859,39 @@ Test: "MiniYaml.Merge, duplicate values found for the following keys: Child2: [Child2 (at test-filename:4),Child2 (at test-filename:5)]")); } + [TestCase(TestName = "Merging may be done on yaml that was not sanitised from comments.")] + public void TestMergeComments() + { + const string BaseYaml = @" +# Random comment +T: + Test2: + MockString: + MockString2: + MockString3: + Child1: + # Random comment + # Random comment + MockString4: + # Random comment +# Random comment +T: + Test2: + MockString: + -MockString2: + MockString3: + # Random comment + -Child1: + # Random comment + MockString4: + # Random comment +# Random comment +"; + + static void Merge() => MiniYaml.Merge(new[] { BaseYaml }.Select(s => MiniYaml.FromString(s, "test-filename", false))); + Assert.That(Merge, Throws.Nothing, "Merging yaml with comments should not throw an exception."); + } + [TestCase(TestName = "Comments are correctly separated from values")] public void TestEscapedHashInValues() {