Skip to content

Deserializing a record instance, causing child elements to be duplicated when using configuration binder Get<T> method #109139

Closed

Description

@rajasekarshanmugam

Description

When deserializing a JSON configuration using the binder via configurationRoot.Get() method, some of the child elements are duplicated. However changing the type from record to class, it works as expected, however using "record" its reproducible. Loading the configuration via json file or json stream does not matter.

Reproduction Steps

Run the below code snippet:

using Microsoft.Extensions.Configuration;
using System.Text.Json;

public static class Program
{
	public static void Main()
	{
		var sample1 = new Root("root1", [new("c1", "c1n1", "c1n1value")]);
		var sampl1Json = sample1.ToJson();
		//Console.WriteLine(sampl1Json);

		var cbjson = new ConfigurationBuilder()
			//.AddJsonFile("appsettings.json", optional: false)
			.AddJsonStream(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(sampl1Json)))
			.Build();

		var sample1copy = cbjson.Get<Root>();
		var sample1copyJson = sample1copy.ToJson();
		Console.WriteLine(sample1copyJson);
	}

	public record Root(string Id, ChildElement[] Elements);

	public record ChildElement(string Id, string Name, string Value);
}

file static class Utils
{
	static readonly JsonSerializerOptions _SERIALIZER = new()
	{
		WriteIndented = true
	};
	public static string ToJson<T>(this T obj) => JsonSerializer.Serialize<T>(obj, _SERIALIZER);
}

Expected behavior

Expected Output:

{
  "Id": "root1",
  "Elements": [
    {
      "Id": "c1",
      "Name": "c1n1",
      "Value": "c1n1value"
    }
  ]
}

Actual behavior

Actual output - notice the dupe element:

{
  "Id": "root1",
  "Elements": [
    {
      "Id": "c1",
      "Name": "c1n1",
      "Value": "c1n1value"
    },
    {
      "Id": "c1",
      "Name": "c1n1",
      "Value": "c1n1value"
    }
  ]
}

Regression?

No response

Known Workarounds

Change the type from a record to a class when using the Get method.

Configuration

Runtimes Checked:
.NET runtime used: .NET 8 - 8.0.10, .NET RC2 - 9.0.100-rc.2.24474.11.

Environment Details:
.NET SDK:
Version: 9.0.100-rc.2.24474.11
Commit: 315e1305db
Workload version: 9.0.100-manifests.cea95dba
MSBuild version: 17.12.0-preview-24473-03+fea15fbd1

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.100-rc.2.24474.11\

Other information

No response

Activity

  1. added
    untriaged
    on Oct 23, 2024
  2. dotnet-policy-service commented on Oct 23, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
    See info in area-owners.md if you want to be subscribed.

  3. elgonzo commented on Oct 23, 2024

    @elgonzo

    This issue has apparently already been fixed, as i cannot reproduce it with the latest pre-release version 9.0.0-rc.2.24473.5 of the Microsoft.Extensions.Configuration.* packages (whereas i could reproduce the issue with the latest 8.0.x packages).

  4. KalleOlaviNiemitalo commented on Oct 23, 2024

    @KalleOlaviNiemitalo

    Duplicate of #83803, which was fixed in #106158 between Microsoft.Extensions.Configuration.Binder 9.0.0-preview.7.24405.7 and 9.0.0-rc.1.24431.7.

  5. dotnet-policy-service commented on Oct 23, 2024

    @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.

  6. tarekgh commented on Oct 23, 2024

    @tarekgh
    Member

    I confirmed the fix. If you want to try it, download the latest net9.0 bits from https://github.com/dotnet/sdk/blob/main/documentation/package-table.md#table. install the version under the 9.0.1xx column.

  7. removed
    untriaged
    on Oct 23, 2024
  8. locked and limited conversation to collaborators on Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions