Fix ConfigurationBinder failing to bind empty array to constructor parameter#126470
Draft
svick wants to merge 2 commits intodotnet:mainfrom
Draft
Fix ConfigurationBinder failing to bind empty array to constructor parameter#126470svick wants to merge 2 commits intodotnet:mainfrom
svick wants to merge 2 commits intodotnet:mainfrom
Conversation
…rameter When binding constructor parameters, the BindingPoint was incorrectly initialized with the config section's string value. For an empty JSON array [], this value is an empty string, which prevented BindInstance from entering the empty-collection handling code path (which requires bindingPoint.Value to be null). For property binding, the BindingPoint is initialized with the existing property value (typically null for uninitialized arrays). Constructor parameters have no existing value, so the initial value should be null (the default), not the config section's string value. The config section's value is already read inside BindInstance from the config parameter directly. Fix dotnet#126312 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes ConfigurationBinder throwing when binding an empty JSON array ([]) to an array-typed constructor parameter (e.g., string[]), by ensuring constructor-parameter binding starts from a null binding point so the existing “empty collection” binding path can run.
Changes:
- Update
BindParameterto stop seeding theBindingPointwith the configuration section’s string value. - Add a regression test covering empty-array binding to an optional array constructor parameter.
- Add a small helper test type used by the new regression test.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs | Adjusts constructor-parameter binding to initialize the BindingPoint without a config-derived initial string value. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs | Adds a regression test for binding [] into an array constructor parameter. |
| src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs | Introduces a helper type with an optional array constructor parameter for the new test. |
Comments suppressed due to low confidence (1)
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs:1146
- In
BindParameter, the local is namedpropertyBindingPoint, but it actually represents a constructor-parameter binding point. Renaming it to something likeparameterBindingPointwould improve readability and avoid confusion with the real property-binding path (BindProperty).
var propertyBindingPoint = new BindingPoint(isReadOnly: false);
BindInstance(
parameter.ParameterType,
propertyBindingPoint,
config.GetSection(parameterName),
...crosoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs
Show resolved
Hide resolved
tarekgh
reviewed
Apr 2, 2026
...crosoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs
Outdated
Show resolved
Hide resolved
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.
Fix #126312
When binding an empty JSON array (
[]) to a constructor parameter of typestring[],ConfigurationBinderthrew:Root Cause
In
BindParameter, theBindingPointwas initialized withconfig.GetSection(parameterName).Value— the config section's string value. For an empty JSON array[], this value is""(empty string). This causedBindInstanceto skip the empty-collection creation code path, which requiresbindingPoint.Valueto benull. The empty string value was then returned as the parameter value, causing theArgumentExceptionwhen the constructor was invoked via reflection.For comparison, property binding correctly initializes the
BindingPointwithproperty.GetValue(instance)(typicallynullfor uninitialized arrays), so the empty-collection path works.Fix
Remove the
initialValueargument from theBindingPointconstructor inBindParameter, so it defaults tonull. This is correct because:BindInstancefrom theconfigparameter directly — passing it asinitialValuewas redundantTryConvertValuesucceeds and overwrites the initial value regardlessTest
Added
TestBindingEmptyArrayToConstructorParameter— reproduces the exact scenario from the issue (empty JSON array bound to astring[]constructor parameter with a null default).