Skip to content

Commit

Permalink
Fix Configuration to throw with ErrorOnUnknownConfiguration option (#…
Browse files Browse the repository at this point in the history
…110209)

* Fix Configuration to throw with ErrorOnUnknownConfiguration option

* Fix the fired Debug.Assert

* address the feedback
  • Loading branch information
tarekgh authored Dec 4, 2024
1 parent f2e5072 commit 73877b4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,23 @@ 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 (!string.IsNullOrEmpty(configValue))
{
// If we don't have an instance, try to create one
// 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));
}
}
else if (isParentCollection && bindingPoint.Value is null)
{
// Try to create the default instance of the type
bindingPoint.TrySetValue(CreateInstance(type, config, options, out _));
}
}
Expand Down Expand Up @@ -498,8 +512,6 @@ private static object CreateInstance(
BinderOptions options,
out ParameterInfo[]? constructorParameters)
{
Debug.Assert(!type.IsArray);

constructorParameters = null;

if (type.IsInterface || type.IsAbstract)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2815,5 +2815,22 @@ 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<string, string?> { ["Values:Monday"] = "not-an-array-of-string" }).Build();
Assert.Throws<InvalidOperationException>(() => configuration.Get<TestSettings>(options => options.ErrorOnUnknownConfiguration = true));

configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string?> { ["Values:Monday"] = "" }).Build();
Assert.Throws<InvalidOperationException>(() => configuration.Get<TestSettings>(options => options.ErrorOnUnknownConfiguration = true));

configuration = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string?> { ["Values:Monday"] = null }).Build();
Assert.Throws<InvalidOperationException>(() => configuration.Get<TestSettings>(options => options.ErrorOnUnknownConfiguration = true));
}

internal class TestSettings { public Dictionary<DayOfWeek, string[]> Values { get; init; } = []; }
#endif
}
}

0 comments on commit 73877b4

Please sign in to comment.