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

[release/8.0-rc2] Ensure Bind can handle null from GetSection #92477

Merged
merged 3 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -298,6 +298,11 @@ private static void BindInstance(
return;
}

if (config is null)
{
return;
}

var section = config as IConfigurationSection;
string? configValue = section?.Value;
if (configValue != null && TryConvertValue(type, configValue, section?.Path, out object? convertedValue, out Exception? error))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -888,5 +888,11 @@ public int MyIntProperty
}
}

public class SimplePoco
{
public string A { get; set; }
public string B { get; set; }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#if BUILDING_SOURCE_GENERATOR_TESTS
using Microsoft.Extensions.Configuration;
#endif
using Microsoft.Extensions.Configuration.Memory;
using Microsoft.Extensions.Configuration.Test;
using Xunit;

Expand Down Expand Up @@ -1767,7 +1768,7 @@ public void EnsureCallingThePropertySetter()
Assert.Equal(0, options.OtherCodeNullable);
Assert.Equal("default", options.OtherCodeString);
Assert.Null(options.OtherCodeNull);
Assert.Null(options.OtherCodeUri);
Assert.Null(options.OtherCodeUri);
}

[Fact]
Expand Down Expand Up @@ -2238,7 +2239,7 @@ void TestUntypedOverloads(IConfiguration? configuration, string? key)
Assert.Throws<ArgumentNullException>(() => configuration.GetValue(typeof(GeolocationClass), key, new GeolocationClass()));
Assert.Throws<ArgumentNullException>(() => configuration.GetValue(typeof(Geolocation), key));
Assert.Throws<ArgumentNullException>(() => configuration.GetValue(typeof(Geolocation), key, defaultValue: null));
Assert.Throws<ArgumentNullException>(() => configuration.GetValue(typeof(Geolocation), key, default(Geolocation)));
Assert.Throws<ArgumentNullException>(() => configuration.GetValue(typeof(Geolocation), key, default(Geolocation)));
}
}

Expand Down Expand Up @@ -2404,5 +2405,38 @@ public void SharedChildInstance()
config.GetSection("A").Bind(instance);
Assert.Equal("localhost", instance.ConnectionString);
}

[Fact]
public void CanBindToMockConfigurationSection()
{
const string expectedA = "hello";

var configSource = new MemoryConfigurationSource()
{
InitialData = new Dictionary<string, string?>()
{
[$":{nameof(SimplePoco.A)}"] = expectedA,
}
};
var configRoot = new MockConfigurationRoot(new[] { configSource.Build(null) });
var configSection = new ConfigurationSection(configRoot, string.Empty);

SimplePoco result = new();
configSection.Bind(result);

Assert.Equal(expectedA, result.A);
Assert.Equal(default(string), result.B);
}

// a mock configuration root that will return null for undefined Sections,
// as is common when Configuration interfaces are mocked
class MockConfigurationRoot : ConfigurationRoot, IConfigurationRoot
{
public MockConfigurationRoot(IList<IConfigurationProvider> providers) : base(providers)
{ }

IConfigurationSection IConfiguration.GetSection(string key) =>
this[key] is null ? null : new ConfigurationSection(this, key);
}
}
}
Loading