From bb6ea3f54e3a456c389cdd81c573cd6b979c400b Mon Sep 17 00:00:00 2001 From: Robert Yokota Date: Fri, 15 Nov 2024 18:21:23 -0800 Subject: [PATCH 1/3] Allow previously parsed schemas to be referenced when parsing a schema --- lang/csharp/src/apache/main/Schema/Schema.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/csharp/src/apache/main/Schema/Schema.cs b/lang/csharp/src/apache/main/Schema/Schema.cs index 3e54653f015..a453ca6a2b3 100644 --- a/lang/csharp/src/apache/main/Schema/Schema.cs +++ b/lang/csharp/src/apache/main/Schema/Schema.cs @@ -238,7 +238,7 @@ public static Schema Parse(string json) /// list of named schemas already read /// enclosing namespace of the schema /// new Schema object - internal static Schema Parse(string json, SchemaNames names, string encspace) + public static Schema Parse(string json, SchemaNames names, string encspace = null) { Schema sc = PrimitiveSchema.NewInstance(json); if (null != sc) return sc; From b9feb4cf23e947ccfa938070ebd76b2ff56a9029 Mon Sep 17 00:00:00 2001 From: Robert Yokota Date: Tue, 19 Nov 2024 00:18:32 -0800 Subject: [PATCH 2/3] Incorporate review feedback --- lang/csharp/src/apache/main/Schema/Schema.cs | 15 ++++++++++++++- .../src/apache/test/Schema/SchemaTests.cs | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lang/csharp/src/apache/main/Schema/Schema.cs b/lang/csharp/src/apache/main/Schema/Schema.cs index a453ca6a2b3..1910fd46cb1 100644 --- a/lang/csharp/src/apache/main/Schema/Schema.cs +++ b/lang/csharp/src/apache/main/Schema/Schema.cs @@ -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 } /// @@ -239,6 +239,19 @@ public static Schema Parse(string json) /// enclosing namespace of the schema /// new Schema object 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 + } + + /// + /// Parses a JSON string to create a new schema object + /// + /// JSON string + /// list of named schemas already read + /// enclosing namespace of the schema + /// new Schema object + internal static Schema ParseInternal(string json, SchemaNames names, string encspace) { Schema sc = PrimitiveSchema.NewInstance(json); if (null != sc) return sc; diff --git a/lang/csharp/src/apache/test/Schema/SchemaTests.cs b/lang/csharp/src/apache/test/Schema/SchemaTests.cs index 319e9a95be3..48fffae6ce9 100644 --- a/lang/csharp/src/apache/test/Schema/SchemaTests.cs +++ b/lang/csharp/src/apache/test/Schema/SchemaTests.cs @@ -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 + 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" })] From 4cd0a9af78df5365a6b79845fb0bfc1f617a2df1 Mon Sep 17 00:00:00 2001 From: Robert Yokota Date: Tue, 19 Nov 2024 00:21:17 -0800 Subject: [PATCH 3/3] Fix comment --- lang/csharp/src/apache/test/Schema/SchemaTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/csharp/src/apache/test/Schema/SchemaTests.cs b/lang/csharp/src/apache/test/Schema/SchemaTests.cs index 48fffae6ce9..221f34eec0d 100644 --- a/lang/csharp/src/apache/test/Schema/SchemaTests.cs +++ b/lang/csharp/src/apache/test/Schema/SchemaTests.cs @@ -413,7 +413,7 @@ 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 + // 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);