Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Configuration to throw with ErrorOnUnknownConfiguration option #110209

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,26 @@ 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 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;
tarekgh marked this conversation as resolved.
Show resolved Hide resolved
}

if (isParentCollection && bindingPoint.Value is null)
{
// If we don't have an instance, try to create one
// Try to create the default instance of the type
bindingPoint.TrySetValue(CreateInstance(type, config, options, out _));
}
}
Expand Down Expand Up @@ -498,8 +515,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
}
}
Loading