Skip to content

Commit ca0d3e4

Browse files
authored
Fix complex types in ctor (#74981)
1 parent b8bf92a commit ca0d3e4

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,12 @@ private static List<PropertyInfo> GetAllProperties([DynamicallyAccessedMembers(D
924924

925925
var propertyBindingPoint = new BindingPoint(initialValue: config.GetSection(parameterName).Value, isReadOnly: false);
926926

927+
BindInstance(
928+
parameter.ParameterType,
929+
propertyBindingPoint,
930+
config.GetSection(parameterName),
931+
options);
932+
927933
if (propertyBindingPoint.Value is null)
928934
{
929935
if (ParameterDefaultValue.TryGetDefaultValue(parameter, out object? defaultValue))
@@ -936,12 +942,6 @@ private static List<PropertyInfo> GetAllProperties([DynamicallyAccessedMembers(D
936942
}
937943
}
938944

939-
BindInstance(
940-
parameter.ParameterType,
941-
propertyBindingPoint,
942-
config.GetSection(parameterName),
943-
options);
944-
945945
return propertyBindingPoint.Value;
946946
}
947947

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ public class DerivedOptionsWithIConfigurationSection : DerivedOptions
250250

251251
public record struct RecordStructTypeOptions(string Color, int Length);
252252

253+
public record RecordOptionsWithNesting(int Number, RecordOptionsWithNesting.RecordNestedOptions Nested1,
254+
RecordOptionsWithNesting.RecordNestedOptions Nested2 = null!)
255+
{
256+
public record RecordNestedOptions(string ValueA, int ValueB);
257+
}
258+
253259
// Here, the constructor has three parameters, but not all of those match
254260
// match to a property or field
255261
public class ClassWhereParametersDoNotMatchProperties
@@ -2356,6 +2362,29 @@ public void CanBindRecordStructOptions()
23562362
Assert.Equal("Green", options.Color);
23572363
}
23582364

2365+
[Fact]
2366+
public void CanBindNestedRecordOptions()
2367+
{
2368+
var dic = new Dictionary<string, string>
2369+
{
2370+
{"Number", "1"},
2371+
{"Nested1:ValueA", "Cool"},
2372+
{"Nested1:ValueB", "42"},
2373+
{"Nested2:ValueA", "Uncool"},
2374+
{"Nested2:ValueB", "24"},
2375+
};
2376+
var configurationBuilder = new ConfigurationBuilder();
2377+
configurationBuilder.AddInMemoryCollection(dic);
2378+
var config = configurationBuilder.Build();
2379+
2380+
var options = config.Get<RecordOptionsWithNesting>();
2381+
Assert.Equal(1, options.Number);
2382+
Assert.Equal("Cool", options.Nested1.ValueA);
2383+
Assert.Equal(42, options.Nested1.ValueB);
2384+
Assert.Equal("Uncool", options.Nested2.ValueA);
2385+
Assert.Equal(24, options.Nested2.ValueB);
2386+
}
2387+
23592388
[Fact]
23602389
public void CanBindOnParametersAndProperties_PropertiesAreSetAfterTheConstructor()
23612390
{

0 commit comments

Comments
 (0)