Skip to content

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

Merged

Conversation

tarekgh
Copy link
Member

@tarekgh tarekgh commented Jan 23, 2023

#79766

Un-mutate the settable collection instances during the binding.

@ghost
Copy link

ghost commented Jan 23, 2023

Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.

Issue Details

#79766

When 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.
The change here is to revert to the .NET 6.0 behavior.

Author: tarekgh
Assignees: -
Labels:

area-Extensions-Configuration

Milestone: -

@tarekgh tarekgh requested a review from eerhardt January 23, 2023 21:23
@tarekgh tarekgh added this to the 8.0.0 milestone Jan 23, 2023
@tarekgh
Copy link
Member Author

tarekgh commented Jan 23, 2023

Copy link
Member

@halter73 halter73 left a 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.

@tarekgh
Copy link
Member Author

tarekgh commented Jan 24, 2023

@halter73 I have scoped the fix to settable collections only if you want to take a second look. Thanks!

@tarekgh tarekgh merged commit 144dbcb into dotnet:main Jan 26, 2023
@tarekgh tarekgh deleted the FixConfigurationBindingWithInstantiatedObjects branch January 26, 2023 20:50
@tarekgh
Copy link
Member Author

tarekgh commented Jan 26, 2023

/backport to release/7.0

@github-actions
Copy link
Contributor

Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/4020016056

@github-actions
Copy link
Contributor

@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!

@github-actions
Copy link
Contributor

@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
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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;
Copy link
Member

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;
Copy link
Member

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;

Copy link
Member Author

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);
Copy link
Member

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);

Copy link
Member Author

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)!;
Copy link
Member

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?

Copy link
Member Author

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.

@ghost ghost locked as resolved and limited conversation to collaborators Mar 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants