Skip to content

Commit

Permalink
AVRO 4091: [C#] Allow previously parsed schemas to be referenced when…
Browse files Browse the repository at this point in the history
… parsing a schema (#3242)

* Allow previously parsed schemas to be referenced when parsing a schema

* Incorporate review feedback

* Fix comment
  • Loading branch information
rayokota authored Nov 20, 2024
1 parent b8e7673 commit b1ec3b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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

0 comments on commit b1ec3b9

Please sign in to comment.