Skip to content

Commit e013f14

Browse files
[release/6.0] Revert bind single elements to array in configuration binder (#59733)
* Revert "Apply feedback from PR #57204 (#57872)" This reverts commit e4a455f. * Revert "Allow ConfigBinder to bind arrays to Singular elements (#57204)" This reverts commit 7f595ce. Co-authored-by: Santiago Fernandez Madero <[email protected]>
1 parent 9e0a252 commit e013f14

File tree

3 files changed

+4
-84
lines changed

3 files changed

+4
-84
lines changed

src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ public static class ConfigurationBinder
1919
private const string TrimmingWarningMessage = "In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.";
2020
private const string InstanceGetTypeTrimmingWarningMessage = "Cannot statically analyze the type of instance so its members may be trimmed";
2121
private const string PropertyTrimmingWarningMessage = "Cannot statically analyze property.PropertyType so its members may be trimmed.";
22-
private const string BindSingleElementsToArraySwitch = "Microsoft.Extensions.Configuration.BindSingleElementsToArray";
23-
24-
// Enable this switch by default.
25-
private static bool ShouldBindSingleElementsToArray { get; } = AppContext.TryGetSwitch(BindSingleElementsToArraySwitch, out bool verifyCanBindSingleElementsToArray) ? verifyCanBindSingleElementsToArray : true;
2622

2723
/// <summary>
2824
/// Attempts to bind the configuration instance to a new instance of type T.
@@ -366,7 +362,7 @@ private static object BindInstance(
366362
return convertedValue;
367363
}
368364

369-
if (config != null && (config.GetChildren().Any() || (configValue != null && ShouldBindSingleElementsToArray)))
365+
if (config != null && config.GetChildren().Any())
370366
{
371367
// If we don't have an instance, try to create one
372368
if (instance == null)
@@ -499,7 +495,7 @@ private static void BindCollection(
499495
Type itemType = collectionType.GenericTypeArguments[0];
500496
MethodInfo addMethod = collectionType.GetMethod("Add", DeclaredOnlyLookup);
501497

502-
foreach (IConfigurationSection section in GetChildrenOrSelf(config))
498+
foreach (IConfigurationSection section in config.GetChildren())
503499
{
504500
try
505501
{
@@ -522,7 +518,7 @@ private static void BindCollection(
522518
[RequiresUnreferencedCode("Cannot statically analyze what the element type is of the Array so its members may be trimmed.")]
523519
private static Array BindArray(Array source, IConfiguration config, BinderOptions options)
524520
{
525-
IConfigurationSection[] children = GetChildrenOrSelf(config).ToArray();
521+
IConfigurationSection[] children = config.GetChildren().ToArray();
526522
int arrayLength = source.Length;
527523
Type elementType = source.GetType().GetElementType();
528524
var newArray = Array.CreateInstance(elementType, arrayLength + children.Length);
@@ -706,26 +702,5 @@ private static string GetPropertyName(MemberInfo property)
706702

707703
return property.Name;
708704
}
709-
710-
private static IEnumerable<IConfigurationSection> GetChildrenOrSelf(IConfiguration config)
711-
{
712-
if (!ShouldBindSingleElementsToArray)
713-
{
714-
return config.GetChildren();
715-
}
716-
717-
IEnumerable<IConfigurationSection> children;
718-
// If configuration's children is an array, the configuration key will be a number
719-
if (config.GetChildren().Any(a => long.TryParse(a.Key, out _)))
720-
{
721-
children = config.GetChildren();
722-
}
723-
else
724-
{
725-
children = new[] { config as IConfigurationSection };
726-
}
727-
728-
return children;
729-
}
730705
}
731-
}
706+
}

src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.ComponentModel;
77
using System.Reflection;
8-
using Microsoft.DotNet.RemoteExecutor;
98
using Xunit;
109

1110
namespace Microsoft.Extensions.Configuration.Binder.Test
@@ -937,59 +936,6 @@ public void ExceptionWhenTryingToBindToByteArray()
937936
exception.Message);
938937
}
939938

940-
[Fact]
941-
public void CanBindSingleElementToCollection()
942-
{
943-
var dic = new Dictionary<string, string>
944-
{
945-
{"MyString", "hello world"},
946-
{"Nested:Integer", "11"},
947-
};
948-
949-
var configurationBuilder = new ConfigurationBuilder();
950-
configurationBuilder.AddInMemoryCollection(dic);
951-
IConfiguration config = configurationBuilder.Build();
952-
953-
var stringArr = config.GetSection("MyString").Get<string[]>();
954-
Assert.Equal("hello world", stringArr[0]);
955-
Assert.Equal(1, stringArr.Length);
956-
957-
var stringAsStr = config.GetSection("MyString").Get<string>();
958-
Assert.Equal("hello world", stringAsStr);
959-
960-
var nested = config.GetSection("Nested").Get<NestedOptions>();
961-
Assert.Equal(11, nested.Integer);
962-
963-
var nestedAsArray = config.GetSection("Nested").Get<NestedOptions[]>();
964-
Assert.Equal(11, nestedAsArray[0].Integer);
965-
Assert.Equal(1, nestedAsArray.Length);
966-
}
967-
968-
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
969-
public void CannotBindSingleElementToCollectionWhenSwitchSet()
970-
{
971-
RemoteExecutor.Invoke(() =>
972-
{
973-
AppContext.SetSwitch("Microsoft.Extensions.Configuration.BindSingleElementsToArray", false);
974-
975-
var dic = new Dictionary<string, string>
976-
{
977-
{"MyString", "hello world"},
978-
{"Nested:Integer", "11"},
979-
};
980-
981-
var configurationBuilder = new ConfigurationBuilder();
982-
configurationBuilder.AddInMemoryCollection(dic);
983-
IConfiguration config = configurationBuilder.Build();
984-
985-
var stringArr = config.GetSection("MyString").Get<string[]>();
986-
Assert.Null(stringArr);
987-
988-
var stringAsStr = config.GetSection("MyString").Get<string>();
989-
Assert.Equal("hello world", stringAsStr);
990-
}).Dispose();
991-
}
992-
993939
private interface ISomeInterface
994940
{
995941
}

src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Microsoft.Extensions.Configuration.Binder.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<PropertyGroup>
44
<TargetFrameworks>$(NetCoreAppCurrent);net461</TargetFrameworks>
55
<EnableDefaultItems>true</EnableDefaultItems>
6-
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
76
</PropertyGroup>
87

98
<ItemGroup>

0 commit comments

Comments
 (0)