Skip to content

Commit

Permalink
Improve handling of ConnectionStrings when the extension method name …
Browse files Browse the repository at this point in the history
…is ConnectionString
  • Loading branch information
almostchristian committed Nov 4, 2022
1 parent 9bd8020 commit 8efb3a8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public StringArgumentValue(IConfigurationSection section, string providedValue,

if (toType == typeof(string))
{
if ("ConnectionString".Equals(providedKey ?? originalKey, StringComparison.OrdinalIgnoreCase))
if ("ConnectionString".Equals(providedKey, StringComparison.OrdinalIgnoreCase) ||
"ConnectionString".Equals(originalKey, StringComparison.OrdinalIgnoreCase))
{
return resolutionContext.RootConfiguration.GetConnectionString(providedValue) ?? providedValue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>1.7.1</Version>
<Version>1.7.2</Version>
<FileVersion>$(Version).$([System.DateTime]::Now.ToString(yy))$([System.DateTime]::Now.DayOfYear.ToString(000))</FileVersion>
<PackageVersion>$(Version)</PackageVersion>
<InformationalVersion>$(FileVersion)-$(GIT_VERSION)</InformationalVersion>
Expand All @@ -23,7 +23,7 @@
<PackageTags>dependencyinjection;configuration;ioc;di;</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>
v1.7.1
v1.7.2
- Added special handling for retrieving ConnectionStrings
- Lambda parameters can now be in any position
v1.6.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,24 @@ public void WithSimpleValue_GivenConnectionString_SetsConnectionStringValue(stri
Assert.Equal(expectedConnectionStringValue, option.Value.ConnectionString);
}

[Theory]
[InlineData("Conn1", "abcd")]
[InlineData("Conn2", "efgh")]
public void WithObjectNotation_CallExtensionForConnectionString_SetsConnectionStringValue(string connectionStringName, string expectedConnectionStringValue)
{
// IConfiguration sorts the keys when calling GetChildren()
var json = @$"
{{
'ConfigurationAction': {{
'ConnectionString': '{connectionStringName}'
}}
}}";

var sp = BuildFromJson(json);
var option = sp.GetService<IOptions<ComplexObject>>();
Assert.Equal(expectedConnectionStringValue, option.Value.Name);
}

private IServiceProvider BuildFromJson(string json)
{
var serviceCollection = ProcessJson(json);
Expand Down
5 changes: 5 additions & 0 deletions tests/TestDummies/DummyServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,10 @@ public static IServiceCollection ConfigureDbConnection(this IServiceCollection s
{
return services.Configure(configure);
}

public static void ConnectionString(this ComplexObject obj, string value)
{
obj.Name = value;
}
}
}

0 comments on commit 8efb3a8

Please sign in to comment.