-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix using right configuration provider order when getting configuration value #117474
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 using right configuration provider order when getting configuration value #117474
Conversation
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
CC @halter73 |
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.
Pull Request Overview
This PR adjusts the configuration lookup so that the most recently added provider takes precedence by iterating providers in reverse, and adds a unit test to verify that missing keys in later providers don’t override earlier values.
- Reverse the provider enumeration in
TryGetConfiguration
to align with standard override semantics. - Introduce
TestProvidersOrder
to ensure missing keys in the last provider retain earlier values.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
src/libraries/Microsoft.Extensions.Configuration/src/InternalConfigurationRootExtensions.cs | Change TryGetConfiguration to build an IList and loop providers in reverse order. |
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs | Add TestProvidersOrder unit test to validate provider precedence when keys are missing. |
Comments suppressed due to low confidence (3)
src/libraries/Microsoft.Extensions.Configuration/src/InternalConfigurationRootExtensions.cs:45
- [nitpick] The comment could be expanded to explain why reverse iteration is needed (last-added provider wins), which will help future maintainers understand the rationale.
// common cases Providers is IList<IConfigurationProvider> in ConfigurationRoot
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs:3012
- [nitpick] The test name
TestProvidersOrder
is generic; consider renaming to something likeGetConfiguration_UsesLastProviderValueUnlessKeyMissing
to clearly convey behavior under test.
public void TestProvidersOrder()
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs:3012
- Add an additional test where a later provider explicitly sets a key to
null
, verifying thatnull
correctly overrides earlier non-null values as intended by the null-support change.
public void TestProvidersOrder()
Fixes #117444
Fixes #117445
Fixes #117469
We recently made a change #116677 to support null values in configuration. Initially, the code was performing the configuration value lookup in the order the providers are stored in the root configuration object. However, to align with standard behavior, the lookup should occur in reverse order, as that's how configuration resolution typically works.
I've manually verified the fix against all reported issues and added a test case to cover this previously missed scenario.