Skip to content

Conversation

@tarekgh
Copy link
Member

@tarekgh tarekgh commented Oct 31, 2025

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.

Copilot AI review requested due to automatic review settings October 31, 2025 15:44
@tarekgh tarekgh added this to the 11.0.0 milestone Oct 31, 2025
@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.

Copy link
Contributor

Copilot AI left a 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 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 IsIEnumerableInterface to detect exact IEnumerable<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

@tarekgh tarekgh requested a review from ericstj October 31, 2025 16:01
Copy link
Member

@jeffhandley jeffhandley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@tarekgh
Copy link
Member Author

tarekgh commented Oct 31, 2025

Could the config have other collection types such as IReadOnlyList where the behavior is the same as IEnumerable and would also have the same unexpected break here?

With this change and using other collections like IReadOnlyList<T>, we'll get .NET 9.0 behavior which will bind to null value instead of empty collection. We can consider improving these cases in .NET 11 to limit the risk if we decide to service this change to .NET 10.

@tarekgh
Copy link
Member Author

tarekgh commented Nov 3, 2025

/ba-g the failures are unrelated and mostly showing in other PRs.

@tarekgh tarekgh merged commit 62b440f into dotnet:main Nov 3, 2025
88 of 95 checks passed
@tarekgh
Copy link
Member Author

tarekgh commented Nov 3, 2025

/backport to release/10.0

@github-actions
Copy link
Contributor

github-actions bot commented Nov 3, 2025

Started backporting to release/10.0 (link to workflow run)

tarekgh added a commit that referenced this pull request Nov 5, 2025
…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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ConfigurationBinder fails to bind empty arrays to nullable IEnumerable properties without default values

3 participants