Skip to content

Commit 8efb3a8

Browse files
Improve handling of ConnectionStrings when the extension method name is ConnectionString
1 parent 9bd8020 commit 8efb3a8

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

src/ConfigurationProcessor.Core/Implementation/StringArgumentValue.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public StringArgumentValue(IConfigurationSection section, string providedValue,
3838

3939
if (toType == typeof(string))
4040
{
41-
if ("ConnectionString".Equals(providedKey ?? originalKey, StringComparison.OrdinalIgnoreCase))
41+
if ("ConnectionString".Equals(providedKey, StringComparison.OrdinalIgnoreCase) ||
42+
"ConnectionString".Equals(originalKey, StringComparison.OrdinalIgnoreCase))
4243
{
4344
return resolutionContext.RootConfiguration.GetConnectionString(providedValue) ?? providedValue;
4445
}

src/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</PropertyGroup>
66

77
<PropertyGroup>
8-
<Version>1.7.1</Version>
8+
<Version>1.7.2</Version>
99
<FileVersion>$(Version).$([System.DateTime]::Now.ToString(yy))$([System.DateTime]::Now.DayOfYear.ToString(000))</FileVersion>
1010
<PackageVersion>$(Version)</PackageVersion>
1111
<InformationalVersion>$(FileVersion)-$(GIT_VERSION)</InformationalVersion>
@@ -23,7 +23,7 @@
2323
<PackageTags>dependencyinjection;configuration;ioc;di;</PackageTags>
2424
<PackageReadmeFile>README.md</PackageReadmeFile>
2525
<PackageReleaseNotes>
26-
v1.7.1
26+
v1.7.2
2727
- Added special handling for retrieving ConnectionStrings
2828
- Lambda parameters can now be in any position
2929
v1.6.1

tests/ConfigurationProcessor.DependencyInjection.UnitTests/ConfigurationBuilderTestsBase.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,24 @@ public void WithSimpleValue_GivenConnectionString_SetsConnectionStringValue(stri
13591359
Assert.Equal(expectedConnectionStringValue, option.Value.ConnectionString);
13601360
}
13611361

1362+
[Theory]
1363+
[InlineData("Conn1", "abcd")]
1364+
[InlineData("Conn2", "efgh")]
1365+
public void WithObjectNotation_CallExtensionForConnectionString_SetsConnectionStringValue(string connectionStringName, string expectedConnectionStringValue)
1366+
{
1367+
// IConfiguration sorts the keys when calling GetChildren()
1368+
var json = @$"
1369+
{{
1370+
'ConfigurationAction': {{
1371+
'ConnectionString': '{connectionStringName}'
1372+
}}
1373+
}}";
1374+
1375+
var sp = BuildFromJson(json);
1376+
var option = sp.GetService<IOptions<ComplexObject>>();
1377+
Assert.Equal(expectedConnectionStringValue, option.Value.Name);
1378+
}
1379+
13621380
private IServiceProvider BuildFromJson(string json)
13631381
{
13641382
var serviceCollection = ProcessJson(json);

tests/TestDummies/DummyServiceCollectionExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,10 @@ public static IServiceCollection ConfigureDbConnection(this IServiceCollection s
298298
{
299299
return services.Configure(configure);
300300
}
301+
302+
public static void ConnectionString(this ComplexObject obj, string value)
303+
{
304+
obj.Name = value;
305+
}
301306
}
302307
}

0 commit comments

Comments
 (0)