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

AVRO 4091: [C#] Allow previously parsed schemas to be referenced when parsing a schema #3242

Merged
merged 3 commits into from
Nov 20, 2024
Merged
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
17 changes: 15 additions & 2 deletions lang/csharp/src/apache/main/Schema/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ internal static Schema ParseJson(JToken jtok, SchemaNames names, string encspace
public static Schema Parse(string json)
{
if (string.IsNullOrEmpty(json)) throw new ArgumentNullException(nameof(json), "json cannot be null.");
return Parse(json.Trim(), new SchemaNames(), null); // standalone schema, so no enclosing namespace
return ParseInternal(json.Trim(), new SchemaNames(), null); // standalone schema, so no enclosing namespace
}

/// <summary>
Expand All @@ -238,7 +238,20 @@ public static Schema Parse(string json)
/// <param name="names">list of named schemas already read</param>
/// <param name="encspace">enclosing namespace of the schema</param>
/// <returns>new Schema object</returns>
internal static Schema Parse(string json, SchemaNames names, string encspace)
public static Schema Parse(string json, SchemaNames names, string encspace = null)
{
if (string.IsNullOrEmpty(json)) throw new ArgumentNullException(nameof(json), "json cannot be null.");
return ParseInternal(json.Trim(), names, encspace); // standalone schema, so no enclosing namespace
}

/// <summary>
/// Parses a JSON string to create a new schema object
/// </summary>
/// <param name="json">JSON string</param>
/// <param name="names">list of named schemas already read</param>
/// <param name="encspace">enclosing namespace of the schema</param>
/// <returns>new Schema object</returns>
internal static Schema ParseInternal(string json, SchemaNames names, string encspace)
{
Schema sc = PrimitiveSchema.NewInstance(json);
if (null != sc) return sc;
Expand Down
19 changes: 19 additions & 0 deletions lang/csharp/src/apache/test/Schema/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,25 @@ public void TestRecordCreationWithRecursiveRecord()
Assert.AreEqual(schema, recordSchema.ToString());
}

[TestCase]
public void TestRecordWithNamedReference()
{
string nestedSchema = "{\"name\":\"NestedRecord\",\"type\":\"record\",\"fields\":[{\"name\":\"stringField\",\"type\":\"string\"}]}";
// The root schema references the nested schema above by name only.
// This mimics tools that allow schemas to have references to other schemas.
string rootSchema = "{\"name\":\"RootRecord\",\"type\":\"record\",\"fields\":[{\"name\": \"nestedField\",\"type\":\"NestedRecord\"}]}";

NamedSchema nestedRecord = (NamedSchema) Schema.Parse(nestedSchema);

SchemaNames names = new SchemaNames();
names.Add(nestedRecord.SchemaName, nestedRecord);

// Pass the schema names when parsing the root schema and its reference.
RecordSchema rootRecord = (RecordSchema) Schema.Parse(rootSchema, names);
Assert.AreEqual("RootRecord", rootRecord.Name);
Assert.AreEqual("NestedRecord", rootRecord.Fields[0].Schema.Name);
}

[TestCase("{\"type\":\"enum\",\"name\":\"Test\",\"symbols\":[\"A\",\"B\"]}",
new string[] { "A", "B" })]

Expand Down