Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
20 changes: 19 additions & 1 deletion lang/csharp/src/apache/main/Schema/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* limitations under the License.
*/
using System;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

Expand Down Expand Up @@ -243,11 +244,28 @@ internal static Schema Parse(string json, SchemaNames names, string encspace)
Schema sc = PrimitiveSchema.NewInstance(json);
if (null != sc) return sc;

// Refer to https://issues.apache.org/jira/browse/AVRO-3856
// Refer to https://github.com/JamesNK/Newtonsoft.Json/pull/2904
// Newtonsoft author advised to use JObject.Load/JArray.Load instead of JObject.Parse()/JArray.Parse()
// The reason is we can set the MaxDepth property on the JsonReader.
JsonReader reader = new JsonTextReader(new StringReader(json));
Comment thread
tradercentric marked this conversation as resolved.
Outdated
// Another issue discovered is JsonReader.Push(JsonContainerType value) method overcounting the depth
// level of Avro schema. Here are the observation of over-counting depth level in Newtonsoft's JsonReader:
// Avro Schema Depth JsonReader Depth Level Count
// 4 11
// 16 44
// 32 92
// 64 188
// So, roughly speaking, the depth level count is about 2.75 times of Avro schema depth.
// Below is the hard-coded value to compensate over-counting of depth level in Newtonsoft
// to support Avro schema depth level to 64 slightly beyond.
Comment thread
tradercentric marked this conversation as resolved.
Outdated
reader.MaxDepth = 192;
Comment thread
tradercentric marked this conversation as resolved.
Outdated

try
{
bool IsArray = json.StartsWith("[", StringComparison.Ordinal)
&& json.EndsWith("]", StringComparison.Ordinal);
JContainer j = IsArray ? (JContainer)JArray.Parse(json) : (JContainer)JObject.Parse(json);
JContainer j = IsArray ? (JContainer)JArray.Load(reader) : (JContainer)JObject.Load(reader);
Comment thread
tradercentric marked this conversation as resolved.
Outdated

return ParseJson(j, names, encspace);
}
Expand Down
Loading