Skip to content

Change ConfigurationBinder to avoid reading properties unnecessarily. - #66723

Merged
tarekgh merged 8 commits into
dotnet:mainfrom
madelson:issue-52905
Apr 2, 2022
Merged

tarekgh merged 8 commits into
dotnet:mainfrom
madelson:issue-52905

Conversation

@madelson

Copy link
Copy Markdown
Contributor

Previously, ConfigurationBinder would eagerly read properties it
encountered and initialize/bind objects for them even when those
reads/initializations could lead to thrown exceptions or unnecessary
work. Now, we defer work until we determine that it will actually
contribute to the final binding result.

Fix #52905
Fix #65710

Previously, ConfigurationBinder would eagerly read properties it
encountered and initialize/bind objects for them even when those
reads/initializations could lead to thrown exceptions or unnecessary
work. Now, we defer work until we determine that it will actually
contribute to the final binding result.

Fix dotnet#52905
Fix dotnet#65710
{
Type genericType = typeof(List<>).MakeGenericType(type.GenericTypeArguments[0]);
object? instance = Activator.CreateInstance(genericType);
object instance = Activator.CreateInstance(genericType)!;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling Activator.CreateInstance on a List can't ever return null, right?

@tarekgh tarekgh Mar 25, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unlikely to return null for List. I am seeing we didn't check for Dictionary<,> too in the next method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I thought; just wanted to highlight the change.

@ghost ghost added area-Extensions-Configuration community-contribution Indicates that the PR has been added by a community member labels Mar 16, 2022
@ghost

ghost commented Mar 16, 2022

Copy link
Copy Markdown

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

Issue Details

Previously, ConfigurationBinder would eagerly read properties it
encountered and initialize/bind objects for them even when those
reads/initializations could lead to thrown exceptions or unnecessary
work. Now, we defer work until we determine that it will actually
contribute to the final binding result.

Fix #52905
Fix #65710

Author: madelson
Assignees: -
Labels:

area-Extensions-Configuration, community-contribution

Milestone: -

}

if (instance is null
&& Nullable.GetUnderlyingType(type) is { } nullableUnderlyingType)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically new functionality. Previously, null mutable structs that were initially null would end up nooping in BindNonScalar. In my mind it makes more sense to treat these the same whether they start as null or not.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me, but I think we should wrap any exceptions thrown from Activator.CreateInstance(nullableUnderlyingType).

            try
            {
                if (Nullable.GetUnderlyingType(type) is { } nullableUnderlyingType)
                {
                    // Activator.CreateInstance only returns null for Nullable<T> types.
                    return Activator.CreateInstance(nullableUnderlyingType)!;
                }

                return Activator.CreateInstance(type)!;
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(SR.Format(SR.Error_FailedToActivate, type), ex);
            }

If we don't believe Activator.CreateInstance only returns null for Nullable<T> types, we should be checking the return values for both calls to Activator.CreateInstance to see if they're null.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should wrap any exceptions thrown from Activator.CreateInstance(nullableUnderlyingType).

Good call. I forgot that this can happen with default struct constructors.

If we don't believe Activator.CreateInstance only returns null for Nullable types, we should be checking the return values for both calls to Activator.CreateInstance to see if they're null.

@halter73 I'll defer to you on this. I vaguely recall reading a C# article years ago that said some COM objects could result in null returns from Activator.CreateInstance. No idea whether that's still true in .NET Core, whether we care if it is, or whether there are other edge cases like that to consider. Unfortunately, I can't find the original source. If we think explicitly handling Nullable<T> is sufficient to guarantee a non-null return, we can just use ! as in your example.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@madelson I tried to look if Activator.CreateInstance can return null for other cases but couldn't know if this is the case. Should we do the null check anyway to be in the safe side?

@halter73 what you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do the null check anyway to be in the safe side?

I'm also unsure if Activator.CreateInstance can return null in other cases. It's the only scenario I see where the docs say CreateInstance will return null, but I think null checking all created instances as the PR currently does (except for with well-known types like List<T>) makes sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Anything else for me to do here @tarekgh @halter73 ?

# Conflicts:
#	src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
@madelson
madelson marked this pull request as ready for review March 18, 2022 20:11
@maryamariyan

Copy link
Copy Markdown
Contributor

Thanks for the PR @madelson, the changes at first glance seem fine, thanks the test cases look good too.

Note for reviewers,
This issue had been reported earlier too and we reverted its fix since it caused a regression.
Link to the PR reverting original fix: #63586
Let's make sure to add a test case for the regression scenario that was reported in #63479 (Seems like a similar use case is already included).

/cc @halter73 would be good to get your review on this as well.

@tarekgh

tarekgh commented Mar 25, 2022

Copy link
Copy Markdown
Member

Just to note, if flipping the order of the properties, this should workaround this issue:

public class Configuration
{
    public IEnumerable<int> ConfigValues { get; set; } = null!;
    public IEnumerable<int> FilteredConfigValues => ConfigValues.Where(p => p > 10);
}

@madelson

Copy link
Copy Markdown
Contributor Author

Let's make sure to add a test case for the regression scenario that was reported in #63479 (Seems like a similar use case is already included).

@maryamariyan I added a new test case explicitly reproducing this issue.

@tarekgh tarekgh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @madelson!

@halter73 @davidfowl are you interested to take a look at this change?

@halter73 halter73 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM aside from the two minor comments.

@tarekgh

tarekgh commented Mar 29, 2022

Copy link
Copy Markdown
Member

@madelson could you please apply @halter73 suggestions to proceed here? Thanks!

@madelson

Copy link
Copy Markdown
Contributor Author

@tarekgh I've applied the changes. One very small question to be resolved here: #66723 (comment)

@tarekgh
tarekgh merged commit 135053e into dotnet:main Apr 2, 2022
@tarekgh

tarekgh commented Apr 2, 2022

Copy link
Copy Markdown
Member

Thanks @madelson for helping with this PR.

@ghost ghost locked as resolved and limited conversation to collaborators May 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Extensions-Configuration community-contribution Indicates that the PR has been added by a community member

Projects

None yet

4 participants