Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace System.Text.Json.Nodes.Tests
Expand Down Expand Up @@ -339,5 +340,25 @@ public static void JsonValue_AsValue()
JsonValue value = node.AsValue();
Assert.Equal(42, value.GetValue<int>());
}

[Theory]
[InlineData(5)]
[InlineData(32)]
public static void Deserialize_JsonNode_RespectsMaxDepth_DeeplyNestedArrays(int maxDepth)
{
var options = new JsonSerializerOptions { MaxDepth = maxDepth };

// Exactly at max depth: should succeed.
string withinDepth = string.Concat(Enumerable.Repeat("[", maxDepth))
+ string.Concat(Enumerable.Repeat("]", maxDepth));
JsonNode? node = JsonSerializer.Deserialize<JsonNode>(withinDepth, options);
Assert.NotNull(node);
Assert.IsType<JsonArray>(node);

// One level beyond max depth: must throw JsonException.
string beyondDepth = string.Concat(Enumerable.Repeat("[", maxDepth + 1))
+ string.Concat(Enumerable.Repeat("]", maxDepth + 1));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<JsonNode>(beyondDepth, options));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,43 @@ public static void SerializeToNode_RespectsMaxDepth(int maxDepth)
Assert.Throws<JsonException>(() => JsonSerializer.SerializeToNode(value, options));
}

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

// Exactly at max depth should succeed.
string withinDepth = string.Concat(Enumerable.Repeat("[", maxDepth)) + string.Concat(Enumerable.Repeat("]", maxDepth));
JsonNode? node = JsonSerializer.Deserialize<JsonNode>(withinDepth, options);
Assert.NotNull(node);

// One level beyond max depth should throw.
string beyondDepth = string.Concat(Enumerable.Repeat("[", maxDepth + 1)) + string.Concat(Enumerable.Repeat("]", maxDepth + 1));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<JsonNode>(beyondDepth, options));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<JsonArray>(beyondDepth, options));
}

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

// Build nested object JSON: {"x":{"x":...{"x":1}...}}
string withinDepth = string.Concat(Enumerable.Repeat("{\"x\":", maxDepth)) + "1" + string.Concat(Enumerable.Repeat("}", maxDepth));
JsonNode? node = JsonSerializer.Deserialize<JsonNode>(withinDepth, options);
Assert.NotNull(node);

string beyondDepth = string.Concat(Enumerable.Repeat("{\"x\":", maxDepth + 1)) + "1" + string.Concat(Enumerable.Repeat("}", maxDepth + 1));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<JsonNode>(beyondDepth, options));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<JsonObject>(beyondDepth, options));
}

public class RecursiveClass
{
public RecursiveClass? Next { get; set; }
Expand Down