Skip to content

Configuration binders differ when collection elements lack scalar values or providers throw #136974

Description

@svick

Description

The reflection-based and source-generated configuration binders handle some collection elements differently. Enabling source-generated binding changes both the resulting collection and whether provider exceptions propagate.

Reproduction steps

Run this console application referencing Microsoft.Extensions.Configuration.Binder, first with configuration binding generation disabled, then enabled:

<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;

var numbers = new ConfigurationBuilder()
    .AddInMemoryCollection(new Dictionary<string, string?>
    {
        ["Numbers:0:Ignored"] = "x",
        ["Numbers:1"] = "7",
    })
    .Build();

Show("Numbers", () => numbers.GetSection("Numbers").Get<int[]>());

var names = new ConfigurationRoot(
    new List<IConfigurationProvider> { new ThrowingProvider() });

Show("Names", () => names.GetSection("Names").Get<string[]>());

static void Show<T>(string name, Func<T[]?> bind)
{
    try
    {
        T[]? values = bind();
        string result = values is null ? "null" : $"[{string.Join(", ", values)}]";
        Console.WriteLine($"{name}: {result}");
    }
    catch (Exception exception)
    {
        Console.WriteLine(
            $"{name}: {exception.GetType().Name}: {exception.Message}");
    }
}

sealed class ThrowingProvider : ConfigurationProvider
{
    public ThrowingProvider()
    {
        Set("Names:0", "unused");
        Set("Names:1", "good");
    }

    public override bool TryGet(string key, out string? value)
    {
        if (string.Equals(key, "Names:0", StringComparison.OrdinalIgnoreCase))
        {
            throw new InvalidOperationException("Provider read failed.");
        }

        return base.TryGet(key, out value);
    }
}

Actual behavior

With the default BinderOptions:

Scenario Reflection binder Source-generated binder
Numeric collection element has children but no scalar value [0, 7] [7]
Provider throws while reading a string collection element [good] Throws InvalidOperationException

In the first case, reflection binding creates a default-valued element, while generated binding skips it.

In the second case, reflection binding suppresses the provider exception and skips the element, while generated binding propagates the exception.

Expected behavior

Both binders should apply the same collection-element and exception-handling policies. This report does not assume either binder's current behavior is necessarily the correct policy.

Notes

These two bugs are not closely related, so feel free to fix each separately. Or I could file separate issues for each of them.

Related issues

Note

This issue was drafted with GitHub Copilot.

Activity

  1. added
    untriaged
    on Nov 4, 2026
  2. dotnet-policy-service commented on Nov 4, 2026

    @dotnet-policy-service
    Contributor

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

  3. added
    source-generator
    on Nov 4, 2026
  4. removed
    untriaged
    on Nov 5, 2026
  5. added this to the 12.0.0 milestone on Nov 5, 2026
  6. Hack15951 commented on Nov 24, 2026

    @Hack15951

    Hi @rzikm / @dotnet/area-extensions-configuration — I see this is in the 12.0.0 milestone. Is it still open for external contribution? I'd like to take it and add a third boundary case (provider throwing mid-collection) if nobody is working on it.

  7. mrek-msft commented on Nov 24, 2026

    @mrek-msft
    Member

    @Hack15951 Yes, it is open for external contributors. cc @svick for evaluation if this is desired approach and if breaking change process needs to be followed.

  8. Hack15951 commented on Nov 24, 2026

    @Hack15951

    Thanks @mrek-msft. @svick — before I start, could you confirm which behavior should be the baseline? The issue doesn't assume either binder is correct: should the source-generated binder be changed to match reflection (skip elements without scalar values / suppress provider exceptions), or should both be aligned to a new third behavior? Also, if this counts as a breaking change for 12.0, could you point me to the process to follow? I'll prepare the repro and a third boundary case (provider throwing mid-collection) in the meantime.

  9. svick commented on Nov 24, 2026

    @svick
    MemberAuthor

    @Hack15951 Thanks. In this case, I think returning only valid valid values for the first case and rethrowing the exception for the second case are the better behaviors.

    Throwing an exception in a situation that didn't throw before is usually considered a breaking change, so I'm marking the issue as such. The breaking change process only happens after the PR is merged and is usually done by us, so you don't need to do anything.

  10. added
    breaking-change
    on Nov 24, 2026
  11. added
    needs-breaking-change-doc-created
    on Nov 24, 2026
  12. dotnet-policy-service commented on Nov 24, 2026

    @dotnet-policy-service
    Contributor

    Added needs-breaking-change-doc-created label because this issue has the breaking-change label.

    1. Create and link to this issue a matching issue in the dotnet/docs repo using the breaking change documentation template, then remove this needs-breaking-change-doc-created label.

    Tagging @dotnet/compat for awareness of the breaking change.

  13. Hack15951 commented on Nov 25, 2026

    @Hack15951

    Thanks @svick — clear. I'll align the source-generated binder to return only valid values for case 1 and the reflection binder to rethrow for case 2, with a regression test covering the provider-throwing-mid-collection case. Understood that the breaking-change doc is handled by the team after merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Configurationbreaking-changebugneeds-breaking-change-doc-createdsource-generator

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions