Skip to content

Fix ErrorOnUnknownConfiguration to respect ConfigurationKeyNameAttribute#125876

Open
haltandcatchwater wants to merge 1 commit intodotnet:mainfrom
haltandcatchwater:fix/error-on-unknown-config-respects-key-name
Open

Fix ErrorOnUnknownConfiguration to respect ConfigurationKeyNameAttribute#125876
haltandcatchwater wants to merge 1 commit intodotnet:mainfrom
haltandcatchwater:fix/error-on-unknown-config-respects-key-name

Conversation

@haltandcatchwater
Copy link

Note

This contribution was authored with the assistance of AI (Claude Code).

Resolves #119114. Following up on diagnosis by @geekninjaa.

Summary

When ErrorOnUnknownConfiguration is enabled, BindProperties builds a HashSet<string> of known property names to validate configuration keys against. This set was built using PropertyInfo.Name, ignoring any [ConfigurationKeyName] attribute on the property.

This caused a false InvalidOperationException when 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 GetPropertyName helper which already resolves ConfigurationKeyNameAttribute. This aligns the reflection-based binder with the source generator path, which already handles this correctly via ConfigurationKeyName in its key cache.

Changes

  • ConfigurationBinder.cs: Replace mp.Name with GetPropertyName method group reference in the ErrorOnUnknownConfiguration validation block
  • ConfigurationBinderTests.cs: Add test verifying that ErrorOnUnknownConfiguration does not throw when config keys match ConfigurationKeyNameAttribute values

Test Plan

  • New test DoesNotThrowWhenConfigKeyMatchesConfigurationKeyNameAttribute validates the fix using the existing ComplexOptions class which has [ConfigurationKeyName("Named_Property")] on its NamedProperty property
  • Existing ErrorOnUnknownConfiguration tests continue to pass (CI)
  • Existing CanBindConfigurationKeyNameAttributes test continues to pass (CI)

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
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 20, 2026
@dotnet-policy-service
Copy link
Contributor

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

@haltandcatchwater haltandcatchwater marked this pull request as ready for review March 21, 2026 05:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ErrorOnUnknownConfiguration ignores ConfigurationKeyName

1 participant