Skip to content

Commit ff6b9ff

Browse files
Open and Close variable identifiers now properly used
1 parent 3816573 commit ff6b9ff

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

C9S.Extensions.Configuration/ConfigurationRootExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ private static void GetAllConfigurationSections(IEnumerable<IConfigurationSectio
2727

2828
private static void VariableResolver(IConfiguration Configuration, string open, string close)
2929
{
30-
var variableRegex = new Regex($@"({open})(?!.*{open})(?<var>[^{close}]+)\{close}");
30+
var open_esc = Regex.Escape(open);
31+
var close_esc = Regex.Escape(close);
32+
var variableRegex = new Regex($@"({open_esc})(?!.*{open_esc})(?<var>[^{close_esc}]+)\{close_esc}");
3133
while (configSections.Any())
3234
{
3335
var currentSection = configSections.First();
@@ -58,7 +60,7 @@ private static void VariableResolver(IConfiguration Configuration, string open,
5860
section = section.GetSection(sec);
5961
}
6062

61-
currentSection.Value = currentSection.Value.Replace($"{{{{{variable}}}}}", section.Value ?? section[key]);
63+
currentSection.Value = currentSection.Value.Replace($"{open}{variable}{close}", section.Value ?? section[key]);
6264
configSections.Add(currentSection);
6365
}
6466
}

Samples/VariableInVariable/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static void Main(string[] args)
1515

1616
var configuration = builder.Build();
1717

18-
configuration.ResolveVariables();
18+
configuration.ResolveVariables("${", "}");
1919

2020
Console.WriteLine(configuration.GetSection("Auth:ClientID"));
2121
}

Samples/VariableInVariable/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
},
88
"Auth":
99
{
10-
"ClientID": "{{App.{{ASPNETCORE_ENVIRONMENT}}.ClientId}}"
10+
"ClientID": "${App.${ASPNETCORE_ENVIRONMENT}.ClientId}"
1111
}
1212
}

0 commit comments

Comments
 (0)