Skip to content

Duplicate List<T> items when binding class with default constructor containing params keyword to IConfiguration #129760

Description

@DawidBartniczak

Description

Reading JSON configuration file (appsettings.json) and binding it to a class with default constructor containing params keyword using ConfigurationBinder.Get<T> results in a duplicate list items.

Reproduction Steps

  1. Create a class with default constructor containing params keyword and a getter only List field that has the value from default constructor assigned
  2. Create a section in appsettings.json
  3. Bind that section to the class
  4. The resulting list has a duplicated item

Example that triggers the bug

public sealed record ErpConfigurationOptions(
    NexoConfiguration? NexoConfiguration = null,
    BlackBaseConfiguration? BlackBaseConfiguration = null)
{
    public const string SectionName = "ErpConfiguration";
}

public class BlackBaseConfiguration(params List<BlackBaseInstance> instances)
{
    public List<BlackBaseInstance> Instances { get; } = instances;
}

public record BlackBaseInstance(int Id, string InstanceName, char Symbol, string DbfDirectory);
var erpConfig = configuration.GetSection(ErpConfigurationOptions.SectionName)
  .Get<ErpConfigurationOptions>() ?? throw new InvalidOperationException("ERP configuration is missing.");
{
  "ErpConfiguration": {
    "NexoConfiguration": {
      "SqlConnectionString": ""
    },
    "BlackBaseConfiguration": {
      "Instances": [
        {
          "Id": 1,
          "InstanceName": "Poznan",
          "Symbol": "P",
          "DbfDirectory": "/mnt/dbf"
        }
      ]
    }
  }
}
Screenshot

Expected behavior

No duplicate should occur.

Actual behavior

A duplicated list item is read from JSON config which only contains one list item.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

Microsoft.Extensions.Configuration v10.0.9

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions