From 9ed235865632f58ed3514385f4df49545acd735b Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 26 Nov 2024 16:05:52 -0800 Subject: [PATCH 1/3] Fix Configuration to throw with ErrorOnUnknownConfiguration option --- .../src/ConfigurationBinder.cs | 9 +++++++-- .../tests/Common/ConfigurationBinderTests.cs | 11 +++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs index c966e522be1517..63eacc1944a67c 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs @@ -465,9 +465,14 @@ private static void BindInstance( } else { - if (isParentCollection && bindingPoint.Value is null && string.IsNullOrEmpty(configValue)) + // Reaching this point indicates that the configuration section is a leaf node with a string value. + // Typically, configValue will be an empty string if the value in the configuration is empty or null. + // While configValue could be any other string, we already know it cannot be converted to the required type, as TryConvertValue has already failed. + if (isParentCollection && bindingPoint.Value is null && (string.IsNullOrEmpty(configValue) || options.ErrorOnUnknownConfiguration)) { - // If we don't have an instance, try to create one + // If configValue is an empty string, we attempt to create a default instance of the required type. + // If configValue is not an empty string and options.ErrorOnUnknownConfiguration is true, + // we continue processing the configuration and throw an exception at the appropriate point. bindingPoint.TrySetValue(CreateInstance(type, config, options, out _)); } } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs index ef822713f367d9..7e362672051c8a 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs @@ -2815,5 +2815,16 @@ public void CanGetEnumerableNotCollection() Assert.True(result.Enabled); Assert.Equal(new [] { "new", "class", "rosebud"}, result.Keywords); } + +#if !BUILDING_SOURCE_GENERATOR_TESTS + [Fact] + public void EnsureThrowingWithCollectionAndErrorOnUnknownConfigurationOption() + { + var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["Values:Monday"] = "not-an-array-of-string" }).Build(); + Assert.Throws(() => configuration.Get(options => options.ErrorOnUnknownConfiguration = true)); + } + + internal class TestSettings { public Dictionary Values { get; init; } = []; } +#endif } } From 432ac2eadbf90d76e5a6490081ce42866acf2979 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Wed, 27 Nov 2024 15:29:28 -0800 Subject: [PATCH 2/3] Fix the fired Debug.Assert --- .../src/ConfigurationBinder.cs | 22 ++++++++++++++----- .../tests/Common/ConfigurationBinderTests.cs | 6 +++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs index 63eacc1944a67c..bec9a328ae2041 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs @@ -468,11 +468,23 @@ private static void BindInstance( // Reaching this point indicates that the configuration section is a leaf node with a string value. // Typically, configValue will be an empty string if the value in the configuration is empty or null. // While configValue could be any other string, we already know it cannot be converted to the required type, as TryConvertValue has already failed. - if (isParentCollection && bindingPoint.Value is null && (string.IsNullOrEmpty(configValue) || options.ErrorOnUnknownConfiguration)) + + if (!string.IsNullOrEmpty(configValue)) + { + // If we have a value, but no children, we can't bind it to anything + // We already tried calling TryConvertValue and couldn't convert the configuration value to the required type. + if (options.ErrorOnUnknownConfiguration) + { + Debug.Assert(section is not null); + throw new InvalidOperationException(SR.Format(SR.Error_FailedBinding, section.Path, type)); + } + + return; + } + + if (isParentCollection && bindingPoint.Value is null) { - // If configValue is an empty string, we attempt to create a default instance of the required type. - // If configValue is not an empty string and options.ErrorOnUnknownConfiguration is true, - // we continue processing the configuration and throw an exception at the appropriate point. + // Try to create the default instance of the type bindingPoint.TrySetValue(CreateInstance(type, config, options, out _)); } } @@ -503,8 +515,6 @@ private static object CreateInstance( BinderOptions options, out ParameterInfo[]? constructorParameters) { - Debug.Assert(!type.IsArray); - constructorParameters = null; if (type.IsInterface || type.IsAbstract) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs index 7e362672051c8a..95be3ecb5251d2 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs @@ -2822,6 +2822,12 @@ public void EnsureThrowingWithCollectionAndErrorOnUnknownConfigurationOption() { var configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["Values:Monday"] = "not-an-array-of-string" }).Build(); Assert.Throws(() => configuration.Get(options => options.ErrorOnUnknownConfiguration = true)); + + configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["Values:Monday"] = "" }).Build(); + Assert.Throws(() => configuration.Get(options => options.ErrorOnUnknownConfiguration = true)); + + configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary { ["Values:Monday"] = null }).Build(); + Assert.Throws(() => configuration.Get(options => options.ErrorOnUnknownConfiguration = true)); } internal class TestSettings { public Dictionary Values { get; init; } = []; } From ba26e2db5f3fa072261962baf42862fccba634d9 Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Tue, 3 Dec 2024 10:45:49 -0800 Subject: [PATCH 3/3] address the feedback --- .../src/ConfigurationBinder.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs index bec9a328ae2041..69c5382f8dc620 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs @@ -478,11 +478,8 @@ private static void BindInstance( Debug.Assert(section is not null); throw new InvalidOperationException(SR.Format(SR.Error_FailedBinding, section.Path, type)); } - - return; } - - if (isParentCollection && bindingPoint.Value is null) + else if (isParentCollection && bindingPoint.Value is null) { // Try to create the default instance of the type bindingPoint.TrySetValue(CreateInstance(type, config, options, out _));