Skip to content

Commit 581b5e6

Browse files
authored
Fix IndexOutOfRangeException in Configuration to HOCON adaptor (#632)
1 parent 14406c0 commit 581b5e6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/Akka.Hosting.Tests/Configuration/ConfigurationHoconAdapterTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public ConfigurationHoconAdapterTest()
6767
Environment.SetEnvironmentVariable("akka__test_value_4__1", "one");
6868
Environment.SetEnvironmentVariable("akka__actor__serialization_bindings2__\"System.Object\"", "hyperion");
6969

70+
// Issue #631
71+
// Double underscore cases
72+
Environment.SetEnvironmentVariable("__MISE_SESSION", "some-random-string");
73+
// Quadruple underscore cases
74+
Environment.SetEnvironmentVariable("MISE____SESSION", "some-random-string");
75+
Environment.SetEnvironmentVariable("____MISE_SESSION", "some-random-string");
76+
7077
using var stream = new MemoryStream(Encoding.UTF8.GetBytes(ConfigSource));
7178
_root = new ConfigurationBuilder()
7279
.AddJsonStream(stream)
@@ -96,6 +103,10 @@ public Task DisposeAsync()
96103
Environment.SetEnvironmentVariable("akka__test_value_4__1", null);
97104
Environment.SetEnvironmentVariable("akka__actor__serialization_bindings2__\"System.Object\"", null);
98105

106+
Environment.SetEnvironmentVariable("__MISE_SESSION", null);
107+
Environment.SetEnvironmentVariable("MISE____SESSION", null);
108+
Environment.SetEnvironmentVariable("____MISE_SESSION", null);
109+
99110
return Task.CompletedTask;
100111
}
101112

@@ -139,6 +150,11 @@ public void EnvironmentVariableTest()
139150
bindings.ContainsKey("System.Object").Should().BeFalse();
140151
bindings.ContainsKey("system.object").Should().BeTrue();
141152
bindings["system.object"].GetString().Should().Be("hyperion");
153+
154+
// empty string keyed objects should be accessible by using "__"
155+
config.GetString("__.mise-session").Should().Be("some-random-string");
156+
config.GetString("mise.__.session").Should().Be("some-random-string");
157+
config.GetString("__.__.mise-session").Should().Be("some-random-string");
142158
}
143159

144160
[Fact(DisplayName = "Non-normalized adaptor should read environment variable sourced configuration correctly")]
@@ -179,6 +195,11 @@ public void EnvironmentVariableCaseSensitiveTest()
179195
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
180196
bindings.ContainsKey("System.Object").Should().BeTrue();
181197
bindings["System.Object"].GetString().Should().Be("hyperion");
198+
199+
// empty string keyed objects should be accessible by using "__"
200+
config.GetString("__.MISE-SESSION").Should().Be("some-random-string");
201+
config.GetString("MISE.__.SESSION").Should().Be("some-random-string");
202+
config.GetString("__.__.MISE-SESSION").Should().Be("some-random-string");
182203
}
183204

184205
[Fact(DisplayName = "Non-normalized Adaptor should read quote enclosed key inside JSON settings correctly")]

src/Akka.Hosting/Configuration/ConfigurationHoconAdapter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ private static HoconValue ExpandKey(this IConfigurationSection config, HoconObje
4444
var sanitized = (normalizeKeys ? config.Key.ToLowerInvariant() : config.Key).Replace("_", "-");
4545
var keys = sanitized.SplitDottedPathHonouringQuotes().ToList();
4646

47+
// HOCON does not support objects with empty key name.
48+
// The most probable source for this is a double underscore prefixed environment variable, which
49+
// confuses MS.EXT.Configuration.EnvironmentVariables. Just create a temporary name for it.
50+
if(keys.Count == 0)
51+
return parent.GetOrCreateKey("__");
52+
4753
// No need to expand the chain
4854
if (keys.Count == 1)
4955
{

0 commit comments

Comments
 (0)