Skip to content
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 @@ -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 @@ -12,6 +12,7 @@
using Microsoft.Extensions.Configuration;
#endif
using Microsoft.Extensions.Configuration.Test;
using Moq;
using Xunit;

namespace Microsoft.Extensions
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,29 @@ public void SharedChildInstance()
config.GetSection("A").Bind(instance);
Assert.Equal("localhost", instance.ConnectionString);
}

// Moq heavily utilizes RefEmit, which does not work on most aot workloads
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsReflectionEmitSupported))]
public void CanBindToMockConfiugrationSection()
{
const string expectedA = "hello";

var mockConfSection = new Mock<IConfigurationSection>();
var aSection = new Mock<IConfigurationSection>();
aSection.Setup(m => m.Value).Returns(expectedA);

// only mock one of the two properties, the other will return a null section.
// runtime binder uses GetSection
mockConfSection.Setup(config => config.GetSection(nameof(SimplePoco.A))).Returns(aSection.Object);
// source gen uses indexer
mockConfSection.Setup(config => config[nameof(SimplePoco.A)]).Returns(aSection.Object?.Value);
mockConfSection.Setup(config => config.GetChildren()).Returns(new[] { aSection.Object });

SimplePoco result = new();
mockConfSection.Object.Bind(result);

Assert.Equal(expectedA, result.A);
Assert.Equal(default(string), result.B);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Options.ConfigurationExtensions\src\Microsoft.Extensions.Options.ConfigurationExtensions.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="..\..\src\Microsoft.Extensions.Configuration.Binder.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="..\..\gen\Microsoft.Extensions.Configuration.Binder.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Json\src\Microsoft.Extensions.Configuration.Json.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.FileProviders.Abstractions\src\Microsoft.Extensions.FileProviders.Abstractions.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="..\..\src\Microsoft.Extensions.Configuration.Binder.csproj" SkipUseReferenceAssembly="true" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
</ItemGroup>

</Project>