Allow empty strings to convert to nullable
This commit is contained in:
Родитель
ccd817d706
Коммит
cb271d3524
|
@ -382,6 +382,10 @@ namespace Microsoft.Extensions.Configuration
|
|||
{
|
||||
if (type.GetTypeInfo().IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return ConvertValue(Nullable.GetUnderlyingType(type), value);
|
||||
}
|
||||
|
||||
|
|
|
@ -152,6 +152,21 @@ namespace Microsoft.Extensions.Configuration.Binder.Test
|
|||
Assert.Equal(null, options.Section.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void EmptyStringIsNullable()
|
||||
{
|
||||
var dic = new Dictionary<string, string>
|
||||
{
|
||||
{"empty", ""},
|
||||
};
|
||||
var configurationBuilder = new ConfigurationBuilder();
|
||||
configurationBuilder.AddInMemoryCollection(dic);
|
||||
var config = configurationBuilder.Build();
|
||||
|
||||
Assert.Null(config.GetValue<bool?>("empty"));
|
||||
Assert.Null(config.GetValue<int?>("empty"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetScalarNullable()
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче