Fix binding IEnumerable<T> with empty array configuration#121249
Fix binding IEnumerable<T> with empty array configuration#121249tarekgh merged 1 commit intodotnet:mainfrom
Conversation
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes the binding behavior for empty arrays in configuration to nullable IEnumerable<T> properties and the IEnumerable<T> interface type. Previously, empty arrays in configuration would not properly bind to nullable IEnumerable<T> properties, leaving them null instead of assigning an empty array.
Key Changes
- Extended the empty array binding logic to support the
IEnumerable<T>interface in addition to arrays - Added a new helper method
IsIEnumerableInterfaceto detect exactIEnumerable<T>interface types - Updated both the runtime binder and source generator to handle this case
- Fixed a typo in a comment ("configuation" → "configuration")
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| ConfigurationBinder.cs | Added IsIEnumerableInterface method and updated binding logic to handle empty arrays for IEnumerable<T> properties |
| TypeSpec.cs | Added IsExactIEnumerableOfT property to identify IEnumerable<T> interface types in source generation |
| CoreBindingHelpers.cs | Updated source generator to handle empty array assignment for IEnumerable<T> properties |
| KnownTypeSymbols.cs | Fixed typo in comment: "configuation" → "configuration" |
| ConfigurationBinderTests.TestClasses.cs | Added test class MyOptionsWithNullableEnumerable with nullable IEnumerable<T> and array properties |
| ConfigurationBinderTests.cs | Added test case to verify empty array binding to nullable IEnumerable<T> properties |
...crosoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs
Show resolved
Hide resolved
jeffhandley
left a comment
There was a problem hiding this comment.
I don't know the context well enough to understand why only T[] and IEnumerable<T> is sufficient to handle.
Could the config have other collection types such as IReadOnlyList<T> where the behavior is the same as IEnumerable<T> and would also have the same unexpected break here?
With this change and using other collections like |
src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs
Show resolved
Hide resolved
|
/ba-g the failures are unrelated and mostly showing in other PRs. |
|
/backport to release/10.0 |
|
Started backporting to |
…ion (#121325) Backport of #121249 to release/10.0 /cc @tarekgh ## Customer Impact - [x] Customer reported - [ ] Found internally Applications that use an empty array configuration such as `"IEnumerableProperty": []` and bind it to an uninitialized property of type `IEnumerable<T>`, `IReadOnlyList<T>`, or `IReadOnlyCollection<T>` will encounter an `ArgumentNullException`. This exception can cause the application to crash if it isn’t properly handled. The issue is reported by the issue #121249 ## Regression - [x] Yes - [ ] No The regression was introduced in .NET 10 Preview 7 through PR #116677. ## Testing Tested all potential failure cases manually and ensured no exceptions are thrown. Also added a regression test for the previously failing scenario. ## Risk Low, as the fix was scoped to only affect the specific failing scenario, when an empty list configuration is bound to a property of the specified type. Co-authored-by: Tarek Mahmoud Sayed <tarekms@microsoft.com>
Fixes #121193
The change is fixing IEnumerable type binding to an empty array configuration. The change also ensures the source generator will behave exactly as the runtime binder.