-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Fix Configuration Binding with Instantiated Objects #81050
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
Fix Configuration Binding with Instantiated Objects #81050
Conversation
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsWhen binding read-only collection interface against instantiated object, we shouldn't mutate this object or replace it with another created object. The reason is we cannot create a new collection instance behaving exactly as the instantiated one. For example, users can create a collection (e.g. Set) with specific string comparer behavior.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes the issue described in #79766 worse when the collection type is immutable. If MySet
was declared as an IReadOnlySet
instead of an ISet
, even the "// workaround" would break after this change, since the set will now contain no items after binding.
It's one thing to skip over unsettable collections, but completely skipping over settable collections that we would otherwise set if the initial value was null is going too far in my opinion. I know that this is the pre-7.0 behavior for IEnumerable, but this old behavior was reported as a bug by several customers in #36390. If you actually wanted the binder to skip over the property, you could remove the public setter.
I think the more targeted fix would be to prefer mutating the existing instance if it's declared as a mutable collection type like we did in .NET 6. This should be enough to fix the regression demonstrated in #79766 and be relatively safe to backport.
If we feel strongly that overwriting a non-null, immutable collection is too surprising because it could lose customizations from the original instance, we could throw instead of skip. The exception message could suggest removing the public setter in the unlikely case that the developer really did want to skip binding the collection. That wouldn't be a good candidate for backporting of course.
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Outdated
Show resolved
Hide resolved
@halter73 I have scoped the fix to settable collections only if you want to take a second look. Thanks! |
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Outdated
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Show resolved
Hide resolved
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Outdated
Show resolved
Hide resolved
/backport to release/7.0 |
Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/4020016056 |
@tarekgh backporting to release/7.0 failed, the patch most likely resulted in conflicts: $ git am --3way --ignore-whitespace --keep-non-patch changes.patch
Applying: Fix Configuration Binding with Instantiated Objects
Using index info to reconstruct a base tree...
M src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
M src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs
CONFLICT (content): Merge conflict in src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs
Auto-merging src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Fix Configuration Binding with Instantiated Objects
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128 Please backport manually! |
@tarekgh an error occurred while backporting to release/7.0, please check the run log for details! Error: git am failed, most likely due to a merge conflict. |
@@ -315,12 +315,15 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig | |||
} | |||
|
|||
// for sets and read-only set interfaces, we clone what's there into a new collection, if we can |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment now incorrect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to remove the word sets
from the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the comment needs to be beefed up a bit to describe the whole behavior.
if (!bindingPoint.IsReadOnly && newValue != null) | ||
{ | ||
bindingPoint.SetValue(newValue); | ||
} | ||
} | ||
|
||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't the if (TypeIsADictionaryInterface(type)
section below symmetrical with the above if (TypeIsASetInterface(type)
? It feels wrong that these two checks are different.
@@ -737,32 +748,38 @@ private static Array BindArray(Type type, IEnumerable? source, IConfiguration co | |||
{ | |||
Type elementType = type.GetGenericArguments()[0]; | |||
|
|||
Type keyType = type.GenericTypeArguments[0]; | |||
bool keyTypeIsEnum = elementType.IsEnum; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keyType
is no longer used. Should this be changed to:
bool elementTypeIsEnum = elementType.IsEnum;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes.
{ | ||
foreach (object? item in orig) | ||
dictionaryType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType); | ||
var dictionary = Activator.CreateInstance(dictionaryType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
object? dictionary = Activator.CreateInstance(dictionaryType);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, make sense.
@@ -812,7 +812,7 @@ public void CanBindISetNoSetter() | |||
|
|||
var config = configurationBuilder.Build(); | |||
|
|||
var options = config.Get<ComplexOptions>()!; | |||
var options = config.Get<ComplexOptions>(o => o.ErrorOnUnknownConfiguration = true)!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was this change for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to get the exception thrown instead of skipping the configuration. Just to help in the case of the test failure.
#79766
Un-mutate the settable collection instances during the binding.