From 8efb3a8e08b80521d1385d6a10db8668aaec5e2a Mon Sep 17 00:00:00 2001 From: almostchristian Date: Fri, 4 Nov 2022 22:01:45 +0800 Subject: [PATCH] Improve handling of ConnectionStrings when the extension method name is ConnectionString --- .../Implementation/StringArgumentValue.cs | 3 ++- src/Directory.Build.props | 4 ++-- .../ConfigurationBuilderTestsBase.cs | 18 ++++++++++++++++++ .../DummyServiceCollectionExtensions.cs | 5 +++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/ConfigurationProcessor.Core/Implementation/StringArgumentValue.cs b/src/ConfigurationProcessor.Core/Implementation/StringArgumentValue.cs index 551b99e..af5a505 100644 --- a/src/ConfigurationProcessor.Core/Implementation/StringArgumentValue.cs +++ b/src/ConfigurationProcessor.Core/Implementation/StringArgumentValue.cs @@ -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; } diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 7442acf..e3e64bd 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -5,7 +5,7 @@ - 1.7.1 + 1.7.2 $(Version).$([System.DateTime]::Now.ToString(yy))$([System.DateTime]::Now.DayOfYear.ToString(000)) $(Version) $(FileVersion)-$(GIT_VERSION) @@ -23,7 +23,7 @@ dependencyinjection;configuration;ioc;di; README.md -v1.7.1 +v1.7.2 - Added special handling for retrieving ConnectionStrings - Lambda parameters can now be in any position v1.6.1 diff --git a/tests/ConfigurationProcessor.DependencyInjection.UnitTests/ConfigurationBuilderTestsBase.cs b/tests/ConfigurationProcessor.DependencyInjection.UnitTests/ConfigurationBuilderTestsBase.cs index 6f34eda..7ffce29 100644 --- a/tests/ConfigurationProcessor.DependencyInjection.UnitTests/ConfigurationBuilderTestsBase.cs +++ b/tests/ConfigurationProcessor.DependencyInjection.UnitTests/ConfigurationBuilderTestsBase.cs @@ -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>(); + Assert.Equal(expectedConnectionStringValue, option.Value.Name); + } + private IServiceProvider BuildFromJson(string json) { var serviceCollection = ProcessJson(json); diff --git a/tests/TestDummies/DummyServiceCollectionExtensions.cs b/tests/TestDummies/DummyServiceCollectionExtensions.cs index e6de660..0eb2ff2 100644 --- a/tests/TestDummies/DummyServiceCollectionExtensions.cs +++ b/tests/TestDummies/DummyServiceCollectionExtensions.cs @@ -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; + } } }