Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.
Closed
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 @@ -82,6 +82,7 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig
}

propertyValue = BindInstance(property.PropertyType, propertyValue, config.GetSection(property.Name));

if (propertyValue != null && hasPublicSetter)
{
property.SetValue(instance, propertyValue);
Expand All @@ -90,6 +91,12 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig

private static object BindInstance(Type type, object instance, IConfiguration config)
{
// if binding IConfigurationSection, break early
if (type == typeof(IConfigurationSection))
{
return config;
}

var section = config as IConfigurationSection;
var configValue = section?.Value;
if (configValue != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ComponentModel;
using System.Reflection;
using Microsoft.Extensions.Configuration.Memory;
using Microsoft.Extensions.Primitives;
using Xunit;

namespace Microsoft.Extensions.Configuration.Binder.Test
Expand Down Expand Up @@ -73,6 +74,86 @@ public class GenericOptions<T>
public T Value { get; set; }
}

public class ConfigurationInterfaceOptions
{
public IConfigurationSection Section { get; set; }
}

public class DerivedOptionsWithIConfigurationSection : DerivedOptions
{
public IConfigurationSection DerivedSection { get; set; }
}

[Fact]
public void CanBindIConfigurationSection()
{
var dic = new Dictionary<string, string>
{
{"Section:Integer", "-2"},
{"Section:Boolean", "TRUe"},
{"Section:Nested:Integer", "11"},
{"Section:Virtual", "Sup"}
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();

var options = new ConfigurationInterfaceOptions();
config.Bind(options);

var childOptions = new DerivedOptions();
options.Section.Bind(childOptions);

Assert.True(childOptions.Boolean);
Assert.Equal(-2, childOptions.Integer);
Assert.Equal(11, childOptions.Nested.Integer);
Assert.Equal("Derived:Sup", childOptions.Virtual);

Assert.Equal("Section", options.Section.Key);
Assert.Equal("Section", options.Section.Path);
Assert.Equal(null, options.Section.Value);
}

[Fact]
public void CanBindIConfigurationSectionWithDerivedOptionsSection()
{
var dic = new Dictionary<string, string>
{
{"Section:Integer", "-2"},
{"Section:Boolean", "TRUe"},
{"Section:Nested:Integer", "11"},
{"Section:Virtual", "Sup"},
{"Section:DerivedSection:Nested:Integer", "11"},
{"Section:DerivedSection:Virtual", "Sup"}
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);
var config = configurationBuilder.Build();

var options = new ConfigurationInterfaceOptions();
config.Bind(options);

var childOptions = new DerivedOptionsWithIConfigurationSection();
options.Section.Bind(childOptions);


var childDerivedOptions = new DerivedOptions();
childOptions.DerivedSection.Bind(childDerivedOptions);

Assert.True(childOptions.Boolean);
Assert.Equal(-2, childOptions.Integer);
Assert.Equal(11, childOptions.Nested.Integer);
Assert.Equal("Derived:Sup", childOptions.Virtual);
Assert.Equal(11, childDerivedOptions.Nested.Integer);
Assert.Equal("Derived:Sup", childDerivedOptions.Virtual);

Assert.Equal("Section", options.Section.Key);
Assert.Equal("Section", options.Section.Path);
Assert.Equal("DerivedSection", childOptions.DerivedSection.Key);
Assert.Equal("Section:DerivedSection", childOptions.DerivedSection.Path);
Assert.Equal(null, options.Section.Value);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add one more test where the DerviedOptions that you bind from the first toplevel section, itself has another IConfigurationSection inside of it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I'll get this done shortly.

[Fact]
public void GetScalarNullable()
{
Expand Down