You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit c62993f
Browse filesBrowse the repository at this point in the historyBrowse files
Fix config binding source generator duplicating collection items bound through a constructor parameter (#131092)
## Summary
Fixes#83803.
The Configuration Binding **source generator** duplicated the items of a
collection property that is populated while an instance is created, when
binding a type with a parameterized constructor. For example, given:
```csharp
internal sealed class Source(string Name, IEnumerable<string> Addresses, IList<int> Ints, string[] Strings)
{
public string Name { get; } = Name;
public IEnumerable<string> Addresses { get; } = Addresses;
public IList<int> Ints { get; } = Ints;
public string[] Strings { get; } = Strings;
}
```
binding `ints: [1, 2, 3, 4, 5]` produced `1, 2, 3, 4, 5, 1, 2, 3, 4, 5`.
Arrays and scalars were unaffected; only additive collections
(`IList<T>`, `IEnumerable<T>`, settable collections) duplicated. The
reflection binder was already fixed (#106158, #129775); this brings the
source generator in line.
## Root cause
For a type with a parameterized constructor, the generated code first
calls `Initialize(...)` - which invokes the constructor and populates
the members bound during construction - and then calls `BindCore(...)`,
which re-bound **every** property. Re-binding a collection property
appends to the collection that was already filled, so the items appear
twice.
## Fix
The generated `BindCore` method now takes an optional
`boundThroughConstructor` parameter, emitted only for
parameterized-constructor object types that actually have a re-bindable
property bound during construction. Properties bound in `Initialize` are
deferred into an `if (!boundThroughConstructor)` block, so they are
bound only when the instance was **not** created through `Initialize`
(e.g. `config.Bind(existingInstance)`), matching the reflection binder's
`BindProperties`/`ResetPropertyValue` behavior.
Call sites pass the flag when the instance is (re)created:
- `true` for unconditional construction (`var x = Initialize(...)`),
- a runtime `wasNull` flag for the `x ??= Initialize(...)` case, since
the constructor only runs when the existing value was null,
- a runtime flag tracked by the caller for paths that construct the
instance separately from `BindCore` (dictionary elements).
The fix covers every construct-then-bind path: top-level `Get<T>()`,
nested properties (`??=`), dictionary values, and list/array elements.
"Bound during construction" means both:
- a property that flows through a matching constructor parameter
(records, positional or case-mismatched), and
- a `required`/`init`-only property assigned in the generated object
initializer, even when it has no matching constructor parameter.
The latter also fixes a `CS1717` self-assignment (`x = x`) the generator
previously emitted for a complex or collection `init`/`required`
property in `Initialize`.
No baseline `.generated.txt` changes: the extra parameter/guard only
appear for the affected type shapes, none of which are in the baseline
suite.
## Out of scope
Two related, pre-existing source-generator issues surfaced during review
and are intentionally left for follow-ups:
- #95006 - `CS9035` for a `required` property on a
parameterless-constructor type (the generator never emits an object
initializer there).
- A non-null preset default on an `init` collection is replaced rather
than appended to on the `Get<T>()` path (reflection appends).
> [!NOTE]
> This pull request was authored with the assistance of GitHub Copilot.
---------
Co-authored-by: Copilot <[email protected]>
Copilot-Session: 1a0d7ac2-f2a3-4526-93ab-36bf1a23933f
0 commit comments