Fix ErrorOnUnknownConfiguration to respect ConfigurationKeyNameAttribute#125876
Open
haltandcatchwater wants to merge 1 commit intodotnet:mainfrom
Open
Fix ErrorOnUnknownConfiguration to respect ConfigurationKeyNameAttribute#125876haltandcatchwater wants to merge 1 commit intodotnet:mainfrom
haltandcatchwater wants to merge 1 commit intodotnet:mainfrom
Conversation
The ErrorOnUnknownConfiguration validation in BindProperties built its property name set using PropertyInfo.Name, ignoring any ConfigurationKeyNameAttribute. This caused false InvalidOperationException when configuration keys matched the attribute-specified name but not the C# property name. Use the existing GetPropertyName helper which already resolves ConfigurationKeyNameAttribute, aligning the reflection path with the source generator path that already handles this correctly. Fix dotnet#119114
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
This contribution was authored with the assistance of AI (Claude Code).
Resolves #119114. Following up on diagnosis by @geekninjaa.
Summary
When
ErrorOnUnknownConfigurationis enabled,BindPropertiesbuilds aHashSet<string>of known property names to validate configuration keys against. This set was built usingPropertyInfo.Name, ignoring any[ConfigurationKeyName]attribute on the property.This caused a false
InvalidOperationExceptionwhen a configuration key matched the attribute-specified name (e.g.,"Foo") but not the C# property name (e.g.,Bar).The fix uses the existing
GetPropertyNamehelper which already resolvesConfigurationKeyNameAttribute. This aligns the reflection-based binder with the source generator path, which already handles this correctly viaConfigurationKeyNamein its key cache.Changes
ConfigurationBinder.cs: Replacemp.NamewithGetPropertyNamemethod group reference in theErrorOnUnknownConfigurationvalidation blockConfigurationBinderTests.cs: Add test verifying thatErrorOnUnknownConfigurationdoes not throw when config keys matchConfigurationKeyNameAttributevaluesTest Plan
DoesNotThrowWhenConfigKeyMatchesConfigurationKeyNameAttributevalidates the fix using the existingComplexOptionsclass which has[ConfigurationKeyName("Named_Property")]on itsNamedPropertypropertyErrorOnUnknownConfigurationtests continue to pass (CI)CanBindConfigurationKeyNameAttributestest continues to pass (CI)