Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Added failing test case for issue #37610 - [System.Text.Json] Serializing class that has array of children of the same class throws StackOverflowException#37611

Merged
steveharter merged 6 commits intodotnet:masterfrom
oskardudycz:master
May 16, 2019

Conversation

@oskardudycz
Copy link
Copy Markdown
Contributor

Added failing test case for issue #37610 - Serializing class that has array of children of the same class throws StackOverflowException.

JsonSerializer.ToString should not fail when there is no real cycle reference, and just empty object is created, even if class has array of element of the same class.

…that has array of children of the same class throws StackOverflowException
@dnfclas
Copy link
Copy Markdown

dnfclas commented May 12, 2019

CLA assistant check
All CLA requirements met.

TestClassWithArrayOfElementsOfTheSameClass obj = new TestClassWithArrayOfElementsOfTheSameClass();

//It shouldn't throw when there is no real cycle reference, and just empty object is created
Assert.Null(Record.Exception(() => JsonSerializer.ToString(obj)));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the xunit [ActiveIssue] attribute is used in cases like this.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please update the test to reflect what should happen, and then add [ActiveIssue] to skip it for now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steveharter @ahsonkhan updated accordingly to suggestions.

@ahsonkhan ahsonkhan added this to the 3.0 milestone May 14, 2019
TestClassWithArrayOfElementsOfTheSameClass obj = new TestClassWithArrayOfElementsOfTheSameClass();

//It shouldn't throw when there is no real cycle reference, and just empty object is created
Assert.Throws<StackOverflowException>(() => JsonSerializer.ToString(obj));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are skipping this test anyway, we shouldn't assert that this throws stack overflow exception (that isn't the behavior we want).

We should assert what the comment states, that an empty object is created/returned, or whatever error behavior we expect in this case (maybe throw InvalidOperationException or NotSupported).

Copy link
Copy Markdown
Contributor Author

@oskardudycz oskardudycz May 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, my bad. I misunderstood previous suggestion.

Now it should be fine.


//It shouldn't throw when there is no real cycle reference, and just empty object is created
string json = JsonSerializer.ToString(obj);
Assert.Equal(@"{}", json);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pranavkm, @rynowak - what do you expect the behavior to be here? Is returning an empty object JSON fine or should the serializer throw ArgumentException/InvalidOperationException?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty object for certain. This is a legal and useful way to define a data-type 👍

Copy link
Copy Markdown

@ahsonkhan ahsonkhan May 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty object for certain

Is there any value in the output being something like the following instead of {} (if it was feasible)?
{"Array":[]}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be empty object or empty object with inner array initialized with undefined value. For sure it shouldn't be empty array, as property was not initialized.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tried, but does this reproduce with a Dictionary property:

public TestClassWithDictionaryOfElementsOfTheSameClass
{
    public string Dictionary<string, TestClassWithDictionaryOfElementsOfTheSameClass> Values { get; set; }
}

Might be useful to also capture this as a test scenario to make sure this works too.

Copy link
Copy Markdown
Contributor Author

@oskardudycz oskardudycz May 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it's happening for all collections. My initial issue was for arrays (https://github.com/dotnet/corefx/issues/37610) while it's duplicate of the issue with lists (https://github.com/dotnet/corefx/issues/37313)

Copy link
Copy Markdown

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@oskardudycz
Copy link
Copy Markdown
Contributor Author

oskardudycz commented May 16, 2019

Btw. there are lot of other cases related to that - eg. empty array or array initialized but with objects that don't have cycles. So lot of scenarios are currently missed, that should be checked before releasing package, I can add few more if you'd like.

I could also try to provide some fix for that if you give me some hints on where to start.

@steveharter steveharter merged commit 3748331 into dotnet:master May 16, 2019
@ahsonkhan
Copy link
Copy Markdown

I can add few more if you'd like.

Yes, please!

I could also try to provide some fix for that if you give me some hints on where to start.

@steveharter, can you provide some hints or guidance?

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
[System.Text.Json] Serializing class that has array of children of the same class throws StackOverflowException (dotnet/corefx#37611)

Commit migrated from dotnet/corefx@3748331
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants