Skip to content

Commit fd87286

Browse files
authored
Fix issue with forward slashes in JSON keys being converted to colons. (#114)
1 parent e861ff8 commit fd87286

3 files changed

Lines changed: 36 additions & 7 deletions

File tree

src/Winton.Extensions.Configuration.Consul/Extensions/KVPairExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ internal static IEnumerable<KeyValuePair<string, string>> ConvertToConfig(
2222
.Select(
2323
pair =>
2424
{
25-
var key = $"{kvPair.Key.RemoveStart(keyToRemove).TrimEnd('/')}:{pair.Key}"
26-
.Replace('/', ':')
25+
var key = $"{kvPair.Key.RemoveStart(keyToRemove).TrimEnd('/').Replace('/', ':')}:{pair.Key}"
2726
.Trim(':');
2827
if (string.IsNullOrEmpty(key))
2928
{

test/Winton.Extensions.Configuration.Consul.Test/Extensions/KVPairExtensionsTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public ConvertConsulKVPairToConfig()
5555
new object[]
5656
{
5757
"RootKey/Settings",
58-
"RootKey/Settings/Root",
58+
"RootKey/Settings/Root/",
5959
new Dictionary<string, string> { { "Key", "value" } },
6060
new[]
6161
{
@@ -132,6 +132,16 @@ public ConvertConsulKVPairToConfig()
132132
{
133133
new KeyValuePair<string, string>("Section:Key", "value")
134134
}
135+
},
136+
new object[]
137+
{
138+
string.Empty,
139+
"Root/Section",
140+
new Dictionary<string, string> { { "JsonKey/With/Slash", "value" } },
141+
new[]
142+
{
143+
new KeyValuePair<string, string>("Root:Section:JsonKey/With/Slash", "value")
144+
}
135145
}
136146
};
137147

test/Winton.Extensions.Configuration.Consul.Test/Parsers/JsonConfigurationParserTests.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.IO;
23
using System.Text;
34
using FluentAssertions;
@@ -16,14 +17,33 @@ public JsonConfigurationParserTests()
1617

1718
public sealed class Parse : JsonConfigurationParserTests
1819
{
20+
public static IEnumerable<object[]> TestCases => new List<object[]>
21+
{
22+
new object[]
23+
{
24+
"{\"Key\": \"Value\"}",
25+
new Dictionary<string, string> { { "Key", "Value" } }
26+
},
27+
new object[]
28+
{
29+
"{\"parent\": {\"child\": \"Value\"} }",
30+
new Dictionary<string, string?> { { "parent", null }, { "parent:child", "Value" } }
31+
},
32+
new object[]
33+
{
34+
"{\"Key/WithSlash\": \"Value\"}",
35+
new Dictionary<string, string> { { "Key/WithSlash", "Value" } }
36+
}
37+
};
38+
1939
[Theory]
20-
[InlineData("{\"Key\": \"Value\"}", "Key", "Value")]
21-
[InlineData("{\"parent\": {\"child\": \"Value\"} }", "parent:child", "Value")]
22-
private void ShouldParseSimpleJsonFromStream(string json, string key, string expectedValue)
40+
[MemberData(nameof(TestCases))]
41+
private void ShouldParseSimpleJsonFromStream(string json, IDictionary<string, string?> expected)
2342
{
2443
using Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(json));
2544
var result = _parser.Parse(stream);
26-
result[key].Should().Be(expectedValue);
45+
46+
result.Should().BeEquivalentTo(expected);
2747
}
2848
}
2949
}

0 commit comments

Comments
 (0)