Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceHandler.IgnoreCycles not handling duplicate strings properly #51837

Closed
jsheetzati opened this issue Apr 25, 2021 · 3 comments · Fixed by #52253
Closed

ReferenceHandler.IgnoreCycles not handling duplicate strings properly #51837

jsheetzati opened this issue Apr 25, 2021 · 3 comments · Fixed by #52253

Comments

@jsheetzati
Copy link

jsheetzati commented Apr 25, 2021

Description

Using System.Text.Json's ReferenceHandler.IgnoreCycles is not working properly with string properties. It seems that System.Text.Json is improperly detecting strings with the same value as a cycle.

Configuration

System.Text.Json Nuget Version: 6.0.0-preview.3.21201.4

Other information

Here is some sample code that demonstrates the issue. In this case, parent.child.name will be serialized as null. No cycles here so I was expecting the name 'John' to be serialized for both parent.name and parent.child.name.

This same test works fine when not specifying ReferenceHandler and no cyclic exceptions are thrown.

[Fact]
public void Test()
{
	var parent = new Parent
	{
		Name = "John",
		Age = 60,
		Child = new Child
		{
			Name = "John",
			Age = 15
		}
	};

	var json = JsonSerializer.Serialize(parent, new JsonSerializerOptions
	{
		ReferenceHandler = ReferenceHandler.IgnoreCycles,
		WriteIndented = true
	});
}

public class Parent
{
	public string Name { get; set; }

	public int Age { get; set; }

	public Child Child { get; set; }
}

public class Child
{
	public string Name { get; set; }

	public int Age { get; set; }
}

Output without specifying ReferenceHandler

{
  "Name": "John",
  "Age": 60,
  "Child": {
    "Name": "John",
    "Age": 15
  }
}

Output when specifying ReferenceHandler.IgnoreCycles

{
  "Name": "John",
  "Age": 60,
  "Child": {
    "Name": null,
    "Age": 15
  }
}
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Apr 25, 2021
@ghost
Copy link

ghost commented Apr 25, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Using System.Text.Json's ReferenceHandler.IgnoreCycles is not working properly with string properties. It seems that System.Text.Json is improperly detecting strings with the same value as a cycle.

Configuration

System.Text.Json Nuget Version: 6.0.0-preview.3.21201.4

Other information

Here is some sample code that demonstrates the issue. In this case, parent.child.name will be serialized as null. No cycles here so I was expecting the name 'John' to be serialized for both parent.name and parent.child.name.

This same test works fine when not specifying ReferenceHandler and no cyclic exceptions are thrown.

[Fact]
public void Test()
{
	var parent = new Parent
	{
		Name = "John",
		Age = 60,
		Child = new Child
		{
			Name = "John",
			Age = 15
		}
	};

	var json = JsonSerializer.Serialize(parent, new JsonSerializerOptions
	{
		ReferenceHandler = ReferenceHandler.IgnoreCycles,
		WriteIndented = true
	});
}

public class Parent
{
	public string Name { get; set; }

	public int Age { get; set; }

	public Child Child { get; set; }
}

public class Child
{
	public string Name { get; set; }

	public int Age { get; set; }
}
Author: jsheetzati
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@GrabYourPitchforks
Copy link
Member

JSON team - may want to investigate this behavior for other primitives that have reference equality, such as if a single instance of a boxed integer appears in multiple places.

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Apr 26, 2021

Curiously this seem to only impact strings in my tests:

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

void Test(object? value)
{
    var list = new List<object?> { value, value };
    var options = new JsonSerializerOptions { ReferenceHandler = ReferenceHandler.IgnoreCycles };
    var json = JsonSerializer.Serialize(list, options);
    Console.WriteLine(json);
}

Test(42); // [42,42]
Test(new object()); // [{},{}]
Test(new List<int>()); // [[],[]]
Test("John"); // ["John",null]

There seems to be something about strings in particular that makes the serializer erroneously treat all recurring references as cycles. cc @jozkee

@eiriktsarpalis eiriktsarpalis added bug and removed untriaged New issue has not been triaged by the area owner labels Apr 26, 2021
@eiriktsarpalis eiriktsarpalis added this to the 6.0.0 milestone Apr 26, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label May 4, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label May 7, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jun 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants