Skip to content
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

Prefer most derived member in Configuration Binder source generator #101316

Merged
merged 2 commits into from
Apr 29, 2024

Conversation

ericstj
Copy link
Member

@ericstj ericstj commented Apr 19, 2024

Copy link
Contributor

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

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the quick fix here!

Do you think this could be backported to 8.0.x?

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

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

Per #101316 (review) I suspect that this might be introducing a slight regression.

Given that this is a likely candidate for backporting, it might make sense to investigate further.

@ericstj
Copy link
Member Author

ericstj commented Apr 22, 2024

I'll add a test case for an override that only overrides the Getter. We did have a test for the case where a new member only provides a get - in which case we're intentionally not binding.

@ericstj
Copy link
Member Author

ericstj commented Apr 22, 2024

Seems we still have a failure of the Reflection binder:

System.InvalidOperationException : Failed to convert configuration value at 'E' to type 'Microsoft.Extensions.Configuration.Binder.Tests.ConfigurationBinderTests+TestSettingsEnum'.
---- System.FormatException : OptionB is not a valid value for TestSettingsEnum.
-------- System.ArgumentException : Requested value 'OptionB' was not found.

@eerhardt -- does that sound right? That seems to indicate that #101273 might be broken even without the source generator.

@ericstj
Copy link
Member Author

ericstj commented Apr 22, 2024

Interesting: the reflection binder has a different heuristic.

while (baseType != typeof(object))
{
PropertyInfo[] properties = baseType.GetProperties(DeclaredOnlyLookup);
foreach (PropertyInfo property in properties)
{
// if the property is virtual, only add the base-most definition so
// overridden properties aren't duplicated in the list.
MethodInfo? setMethod = property.GetSetMethod(true);
if (setMethod is null || !setMethod.IsVirtual || setMethod == setMethod.GetBaseDefinition())
{
allProperties.Add(property);
}
}
baseType = baseType.BaseType!;
}

It never does any resolution by name, but builds a list in order from derived to base. So it will set both the hidden and non-hidden members through reflection, using the same configuration data. That's unusual behavior - but we could replicate it if we casted the LHS in the source generator to the base type.

@tarekgh
Copy link
Member

tarekgh commented Apr 22, 2024

@ericstj I think we need to keep the runtime behavior for consistency. Even if it looks unusual behavior but seems not many people complained about it.

@eerhardt
Copy link
Member

@eerhardt -- does that sound right? That seems to indicate that #101273 might be broken even without the source generator.

See my comment on #101273 (comment). For the case I hit the issue, the base enum is a superset of the derived enum. The derived class is trying to limit which values can be set.

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Apr 23, 2024

That's unusual behavior - but we could replicate it if we casted the LHS in the source generator to the base type.

Do we want to? Are there tests validating this behavior?

@ericstj
Copy link
Member Author

ericstj commented Apr 23, 2024

I'm not planning to change the reflection binder's behavior in this PR. Nor am I planning to change the source generator to bind many different propreties like the reflection binder does.

I'm inclined to more closely match the reflection-binder's selection algorithm for finding the most-derived property. I think that will be the minimal change while unblocking @eerhardt. I'm running some tests and will make a new change to this PR shortly.

@ericstj
Copy link
Member Author

ericstj commented Apr 29, 2024

Failures are known issues and don't block build or testing.

@ericstj ericstj merged commit 9b4c5c6 into dotnet:main Apr 29, 2024
79 of 84 checks passed
@ericstj
Copy link
Member Author

ericstj commented Apr 29, 2024

/backport to release/8.0-staging

Copy link
Contributor

Started backporting to release/8.0-staging: https://github.com/dotnet/runtime/actions/runs/8881506956

matouskozak pushed a commit to matouskozak/runtime that referenced this pull request Apr 30, 2024
…otnet#101316)

* Prefer most derived member in Configuration Binder source generator

* Skip overridden properties in config source generator - include only definitions
michaelgsharp pushed a commit to michaelgsharp/runtime that referenced this pull request May 9, 2024
…otnet#101316)

* Prefer most derived member in Configuration Binder source generator

* Skip overridden properties in config source generator - include only definitions
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this pull request May 30, 2024
…otnet#101316)

* Prefer most derived member in Configuration Binder source generator

* Skip overridden properties in config source generator - include only definitions
@github-actions github-actions bot locked and limited conversation to collaborators May 31, 2024
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.

ConfigurationBinder source generator generates non-compilable code with new property and internal setter
4 participants