Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

Content loader: an empty map entry is a ContentException, not a NullReferenceException (Fixes #2029) (#2035)

Berif60116823249
diff --git a/changelog.d/2029-empty-map-entry.md b/changelog.d/2029-empty-map-entry.md
new file mode 100644
index 000000000..e033ac072
--- /dev/null
+++ b/changelog.d/2029-empty-map-entry.md
@@ -0,0 +1,3 @@
+### Fixed
+- Content loader: a map entry written with no value (`map_actions: { housing: }`, an empty `budget_owners` line) now
+ fails with the loader's "empty entry" message naming the file and field, not a `NullReferenceException` (#2029).
diff --git a/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs b/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs
index 1f349643c..9aca5c6ff 100644
--- a/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs
+++ b/sim/Statecraft.Sim.Tests/ContentLoaderTests.cs
@@ -346,6 +346,21 @@ targets:
Assert.EndsWith("required", ex.Message);
}
+ // #2029: a map entry written with no value (`housing:`) deserialises to null; it must be a load error naming the
+ // entry, not a NullReferenceException later in validation.
+ [Theory]
+ [InlineData(" housing: { cost: 8, housing: 0.08, morale: 0.01, name: a housing project }", " housing:", "map_actions.housing")]
+ [InlineData("map_actions_per_turn: 2\n", "map_actions_per_turn: 2\nbudget_owners:\n finance_minister:\n", "budget_owners.finance_minister")]
+ public void EmptyMapEntryIsAContentError(string original, string empty, string field)
+ {
+ var bad = Fixture.Files["rules.yaml"].Replace(original, empty);
+ Assert.NotEqual(Fixture.Files["rules.yaml"], bad);
+ var ex = Assert.Throws<ContentException>(() => ContentLoader.Load(Fixture.Write(new Dictionary<string, string> { ["rules.yaml"] = bad })));
+ Assert.Equal("rules.yaml", ex.File);
+ Assert.Equal(field, ex.Field);
+ Assert.EndsWith("empty entry", ex.Message);
+ }
+
[Theory]
[InlineData("office:")]
[InlineData("office: ''")]
diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.cs
index acdbc41a2..dca76f4f4 100644
--- a/sim/Statecraft.Sim/ContentLoader.cs
+++ b/sim/Statecraft.Sim/ContentLoader.cs
@@ -245,6 +245,7 @@ namespace Statecraft.Sim
if (v == null) throw new ContentException(file, key, "must be a map");
foreach (DictionaryEntry kv in (IDictionary)v)
{
+ if (kv.Value == null) throw new ContentException(file, key + "." + kv.Key, "empty entry");
if (kv.Key is string sk) Encodable(file, key + "." + sk, sk);
if (kv.Value is string sv) Encodable(file, key + "." + kv.Key, sv);
}