Skip to content

Add regression test for JsonSerializer.Deserialize<JsonNode> respecting MaxDepth with deeply nested arrays#5

Draft
Copilot wants to merge 2 commits into
cca-eval-controlfrom
copilot/fix-jsonserializer-maxdepth-issue
Draft

Add regression test for JsonSerializer.Deserialize<JsonNode> respecting MaxDepth with deeply nested arrays#5
Copilot wants to merge 2 commits into
cca-eval-controlfrom
copilot/fix-jsonserializer-maxdepth-issue

Conversation

Copilot AI commented May 27, 2026

Copy link
Copy Markdown

JsonSerializer.Deserialize<JsonNode> correctly enforces MaxDepth via Utf8JsonReader's depth tracking in StartArray()/StartObject(), but there was no test covering this deserialization path — only the serialization direction (SerializeToNode) was tested, leaving the door open for regressions.

Adds DeserializeToNode_RespectsMaxDepth to DomTests.cs, mirroring the existing SerializeToNode_RespectsMaxDepth test:

[Theory]
[InlineData(5)]
[InlineData(32)]
[InlineData(70)] // default max depth is 64
public static void DeserializeToNode_RespectsMaxDepth(int maxDepth)
{
    var options = new JsonSerializerOptions { MaxDepth = maxDepth };

    // Exactly at the limit succeeds
    string validJson = string.Concat(Enumerable.Repeat("[", maxDepth))
                     + string.Concat(Enumerable.Repeat("]", maxDepth));
    Assert.NotNull(JsonSerializer.Deserialize<JsonNode>(validJson, options));

    // One level over throws
    string tooDeepJson = string.Concat(Enumerable.Repeat("[", maxDepth + 1))
                       + string.Concat(Enumerable.Repeat("]", maxDepth + 1));
    Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<JsonNode>(tooDeepJson, options));
}

… with deeply nested arrays

Co-authored-by: steveisok <471438+steveisok@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix JsonSerializer.Deserialize<JsonNode> MaxDepth handling Add regression test for JsonSerializer.Deserialize<JsonNode> respecting MaxDepth with deeply nested arrays May 27, 2026
Copilot AI requested a review from steveisok May 27, 2026 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[control] JsonSerializer.Deserialize<JsonNode> does not honor MaxDepth for deeply nested arrays

2 participants