Repository navigation
Microsoft.Extensions.Configuration.Binder: Fix complex types in constructors - #74981
maryamariyan merged 1 commit into
Conversation
Parameters of types which first acquire their value via instance binding (such as simple record types) were not being given the chance to bind before throwing a config missing error erroneously
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsCurrently, the following code: var dic = new Dictionary<string, string>
{
{"BaseInt", "42"},
{"Nested:String", "ABC"},
{"Nested:Int", "1"},
};
var test = new ConfigurationBuilder()
.AddInMemoryCollection(dic)
.Build()
.Get<Test>();
Console.WriteLine(test.ToString());
record Nested(string String, int Int);
record Test(int BaseInt, Nested Nested);produces: However, when giving the The issue is caused by An unintended side effect is that
|
|
|
||
| var propertyBindingPoint = new BindingPoint(initialValue: config.GetSection(parameterName).Value, isReadOnly: false); | ||
|
|
||
| BindInstance( |
There was a problem hiding this comment.
There was a problem hiding this comment.
It seems fine. Test ExceptionWhenTryingToBindToConstructorWithMissingConfig covers the exception case
There was a problem hiding this comment.
@tarekgh - do you have specific exception scenarios in mind? I'm not seeing any.
The only thing I can think of that this may break is default values on the ctor parameter. We should have a test for it, if we don't we should add one.
There was a problem hiding this comment.
The scenario in my head is before the change we used to throw throw new InvalidOperationException(SR.Format(SR.Error_ParameterHasNoMatchingConfig, type, parameterName));. Now with the change, we call BindInstance first which is possible to throw a different exception (like when calling TryConvertValue). So, it is possible the users will get different exceptions. I am not sure if this is a real concern though.
There was a problem hiding this comment.
Looking more, I am seeing we always throw InvalidOperationException. So, i think we fine then. You can ignore this comment.
@Saalvage can you add a test for this case? |
|
@maryamariyan Does the existing test here not fit the bill? |
|
I was thinking of another one with default record that would throw too. Consulted with @eerhardt and he believes this one is sufficient, so merging. |
Currently, the following code:
produces:
System.InvalidOperationException: 'Cannot create instance of type 'Test' because parameter 'Nested' has no matching config. Each parameter in the constructor that does not have a default value must have a corresponding config entry.'
However, when giving the
Nestedproperty a default value ofnull!everything works as expected, including the correct values being printed, suggesting a bug.The issue is caused by
BindParameterprematurely cancelling the binding operator when neitherGetSectionnor a possible default assignment manage to produce a value. This ignores the possibility of a parameter first acquiring a value via theBindInstancecall, as is the case - e.g. - with basic record types (among many others).An unintended side effect is that
BindInstancewill no longer be called after the value has been provided by a defaulted parameter. However, since in that case the types have to match anyways I think the call was previously redundant in that case anyways.