Skip to content

ConfigurationBuilder add elements to initialized array instead of replacing it #36569

@Eilon

Description

@Eilon

From @LordXaosa on Thursday, 18 October 2018 09:39:00

Code:

class TestClass
        {
            public TestClass()
            {
                TestArray = new[] { "123", "test1" };
            }
            public string[] TestArray { get; set; }
        }
class MyConfig
        {
            public TestClass Test{ get; set; }
        }
MyConfig cfg = new MyConfig();
IConfiguration config = new ConfigurationBuilder()
                 .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .AddJsonFile("appsettings.json")
                .Build();
            config.Bind(cfg);

in appsettings.json:

{
  "Test": {
    "TestArray": [ "3", "4" ]
  }
}

I expect after config loading there would be only 2 items: "3" and "4"
But there would be all 4. "123","test1","3","4"
Consider this is a bug or there should be an option for replacing array values.

Copied from original issue: dotnet/aspnetcore#3666

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions