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

Fixes Binding to non-null IEnumerable doesn't work #36390 #66131

Merged
merged 6 commits into from
Mar 17, 2022
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 @@ -2,8 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -378,17 +380,27 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig
{
BindCollection(instance, collectionInterface, config, options);
}
// Something else
else
{
BindNonScalar(config, instance, options);
// See if its an IEnumerable
collectionInterface = FindOpenGenericInterface(typeof(IEnumerable<>), type);
if (collectionInterface != null)
{
instance = BindExistingCollection((IEnumerable)instance!, config, options);
}
// Something else
else
{
BindNonScalar(config, instance, options);
}
}
}
}

return instance;
}


SteveDunn marked this conversation as resolved.
Show resolved Hide resolved
private static object? CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type)
{
if (type.IsInterface || type.IsAbstract)
Expand Down Expand Up @@ -498,6 +510,41 @@ private static void BindCollection(
}
}

[RequiresUnreferencedCode("Cannot statically analyze what the element type is of the object collection so its members may be trimmed.")]
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
private static IEnumerable BindExistingCollection(IEnumerable source, IConfiguration config, BinderOptions options)
{
// find the interface that is IEnumerable<T>
Type type = source.GetType().GetInterface("IEnumerable`1", false)!;
Type elementType = type.GenericTypeArguments[0];
Type genericType = typeof(List<>).MakeGenericType(elementType);

IList newList = (IList)Activator.CreateInstance(genericType, source)!;

IConfigurationSection[] children = config.GetChildren().ToArray();

for (int i = 0; i < children.Length; i++)
{
try
{
object? item = BindInstance(
type: elementType,
instance: null,
config: children[i],
options: options);

if (item != null)
{
newList.Add(item);
}
}
catch
{
}
}

return newList;
}

[RequiresUnreferencedCode("Cannot statically analyze what the element type is of the Array so its members may be trimmed.")]
private static Array BindArray(Array source, IConfiguration config, BinderOptions options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Xunit;

Expand Down Expand Up @@ -47,6 +48,11 @@ public string ReadOnly
{
get { return null; }
}

public IEnumerable<string> NonInstantiatedIEnumerable { get; set; } = null!;
public IEnumerable<string> InstantiatedIEnumerable { get; set; } = new List<string>();
public ICollection<string> InstantiatedICollection { get; set; } = new List<string>();
public IReadOnlyCollection<string> InstantiatedIReadOnlyCollection { get; set; } = new List<string>();
}

public class NestedOptions
Expand Down Expand Up @@ -220,6 +226,107 @@ public void CanBindConfigurationKeyNameAttributes()
Assert.Equal("Yo", options.NamedProperty);
}

[Fact]
SteveDunn marked this conversation as resolved.
Show resolved Hide resolved
public void CanBindNonInstantiatedIEnumerableWithItems()
{
var dic = new Dictionary<string, string>
{
SteveDunn marked this conversation as resolved.
Show resolved Hide resolved
{"NonInstantiatedIEnumerable:0", "Yo1"},
{"NonInstantiatedIEnumerable:1", "Yo2"},
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);

var config = configurationBuilder.Build();

var options = config.Get<ComplexOptions>()!;

Assert.Equal(2, options.NonInstantiatedIEnumerable.Count());
Assert.Equal("Yo1", options.NonInstantiatedIEnumerable.ElementAt(0));
Assert.Equal("Yo2", options.NonInstantiatedIEnumerable.ElementAt(1));
}

[Fact]
public void CanBindInstantiatedIEnumerableWithItems()
{
var dic = new Dictionary<string, string>
{
{"InstantiatedIEnumerable:0", "Yo1"},
{"InstantiatedIEnumerable:1", "Yo2"},
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);

var config = configurationBuilder.Build();

var options = config.Get<ComplexOptions>()!;

Assert.Equal(2, options.InstantiatedIEnumerable.Count());
Assert.Equal("Yo1", options.InstantiatedIEnumerable.ElementAt(0));
Assert.Equal("Yo2", options.InstantiatedIEnumerable.ElementAt(1));
}

[Fact]
public void CanBindInstantiatedICollectionWithItems()
{
var dic = new Dictionary<string, string>
{
{"InstantiatedICollection:0", "Yo1"},
{"InstantiatedICollection:1", "Yo2"},
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);

var config = configurationBuilder.Build();

var options = config.Get<ComplexOptions>()!;

Assert.Equal(2, options.InstantiatedICollection.Count());
Assert.Equal("Yo1", options.InstantiatedICollection.ElementAt(0));
Assert.Equal("Yo2", options.InstantiatedICollection.ElementAt(1));
}

[Fact]
public void CanBindInstantiatedIReadOnlyCollectionWithItems()
{
var dic = new Dictionary<string, string>
{
{"InstantiatedIReadOnlyCollection:0", "Yo1"},
{"InstantiatedIReadOnlyCollection:1", "Yo2"},
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);

var config = configurationBuilder.Build();

var options = config.Get<ComplexOptions>()!;

Assert.Equal(2, options.InstantiatedIReadOnlyCollection.Count);
Assert.Equal("Yo1", options.InstantiatedIReadOnlyCollection.ElementAt(0));
Assert.Equal("Yo2", options.InstantiatedIReadOnlyCollection.ElementAt(1));
}

[Fact]
public void CanBindInstantiatedIEnumerableWithNullItems()
{
var dic = new Dictionary<string, string>
{
{"InstantiatedIEnumerable:0", null},
{"InstantiatedIEnumerable:1", "Yo1"},
{"InstantiatedIEnumerable:2", "Yo2"},
};
var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddInMemoryCollection(dic);

var config = configurationBuilder.Build();

var options = config.Get<ComplexOptions>()!;

Assert.Equal(2, options.InstantiatedIEnumerable.Count());
Assert.Equal("Yo1", options.InstantiatedIEnumerable.ElementAt(0));
Assert.Equal("Yo2", options.InstantiatedIEnumerable.ElementAt(1));
}

[Fact]
public void EmptyStringIsNullable()
{
Expand Down
Loading