Skip to content

Config generator overwrites existing properties when provided with empty config section #91380

Closed

Description

@adamsitnik

In a project that I am working on, we have hierarchical configuration. It means that the parent and child config sections can provide same properties.

Following repro shows the issue:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace ConfigBindRepro
{
    internal class Program
    {
        static void Main()
        {
            HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(settings: null);
            builder.Configuration.AddInMemoryCollection(new KeyValuePair<string, string?>[]
            {
                 new("A:B:ConnectionString", "localhost"),
            });

            AClass instance = new();
            builder.Configuration.GetSection("A:B").Bind(instance); // sets ConnectionString to "localhost"
            builder.Configuration.GetSection("A").Bind(instance); // source gen: sets ConnectionString to null, reflection: does not change the value

            Console.WriteLine($"The value is '{instance.ConnectionString}'");
        }
    }

    public class AClass
    {
        public string? ConnectionString { get; set; }
    }
}

When using reflection based binder:

The value is 'localhost'

When using source gen:

The value is ''

cc @layomia @eerhardt

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Aug 31, 2023
  2. added this to the 8.0.0 milestone on Aug 31, 2023
  3. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Aug 31, 2023
  4. layomia commented on Sep 1, 2023

    @layomia
    Contributor

    Need to resolve alongside #86333; same underlying logic.

  5. removed this from the 8.0.0 milestone on Sep 1, 2023
  6. ghost locked as resolved and limited conversation to collaborators on Oct 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions