Skip to content

Commit b1ec3b9

Browse files
authored
AVRO 4091: [C#] Allow previously parsed schemas to be referenced when parsing a schema (#3242)
* Allow previously parsed schemas to be referenced when parsing a schema * Incorporate review feedback * Fix comment
1 parent b8e7673 commit b1ec3b9

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lang/csharp/src/apache/main/Schema/Schema.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ internal static Schema ParseJson(JToken jtok, SchemaNames names, string encspace
228228
public static Schema Parse(string json)
229229
{
230230
if (string.IsNullOrEmpty(json)) throw new ArgumentNullException(nameof(json), "json cannot be null.");
231-
return Parse(json.Trim(), new SchemaNames(), null); // standalone schema, so no enclosing namespace
231+
return ParseInternal(json.Trim(), new SchemaNames(), null); // standalone schema, so no enclosing namespace
232232
}
233233

234234
/// <summary>
@@ -238,7 +238,20 @@ public static Schema Parse(string json)
238238
/// <param name="names">list of named schemas already read</param>
239239
/// <param name="encspace">enclosing namespace of the schema</param>
240240
/// <returns>new Schema object</returns>
241-
internal static Schema Parse(string json, SchemaNames names, string encspace)
241+
public static Schema Parse(string json, SchemaNames names, string encspace = null)
242+
{
243+
if (string.IsNullOrEmpty(json)) throw new ArgumentNullException(nameof(json), "json cannot be null.");
244+
return ParseInternal(json.Trim(), names, encspace); // standalone schema, so no enclosing namespace
245+
}
246+
247+
/// <summary>
248+
/// Parses a JSON string to create a new schema object
249+
/// </summary>
250+
/// <param name="json">JSON string</param>
251+
/// <param name="names">list of named schemas already read</param>
252+
/// <param name="encspace">enclosing namespace of the schema</param>
253+
/// <returns>new Schema object</returns>
254+
internal static Schema ParseInternal(string json, SchemaNames names, string encspace)
242255
{
243256
Schema sc = PrimitiveSchema.NewInstance(json);
244257
if (null != sc) return sc;

lang/csharp/src/apache/test/Schema/SchemaTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,25 @@ public void TestRecordCreationWithRecursiveRecord()
408408
Assert.AreEqual(schema, recordSchema.ToString());
409409
}
410410

411+
[TestCase]
412+
public void TestRecordWithNamedReference()
413+
{
414+
string nestedSchema = "{\"name\":\"NestedRecord\",\"type\":\"record\",\"fields\":[{\"name\":\"stringField\",\"type\":\"string\"}]}";
415+
// The root schema references the nested schema above by name only.
416+
// This mimics tools that allow schemas to have references to other schemas.
417+
string rootSchema = "{\"name\":\"RootRecord\",\"type\":\"record\",\"fields\":[{\"name\": \"nestedField\",\"type\":\"NestedRecord\"}]}";
418+
419+
NamedSchema nestedRecord = (NamedSchema) Schema.Parse(nestedSchema);
420+
421+
SchemaNames names = new SchemaNames();
422+
names.Add(nestedRecord.SchemaName, nestedRecord);
423+
424+
// Pass the schema names when parsing the root schema and its reference.
425+
RecordSchema rootRecord = (RecordSchema) Schema.Parse(rootSchema, names);
426+
Assert.AreEqual("RootRecord", rootRecord.Name);
427+
Assert.AreEqual("NestedRecord", rootRecord.Fields[0].Schema.Name);
428+
}
429+
411430
[TestCase("{\"type\":\"enum\",\"name\":\"Test\",\"symbols\":[\"A\",\"B\"]}",
412431
new string[] { "A", "B" })]
413432

0 commit comments

Comments
 (0)