This is an automated email from the ASF dual-hosted git repository.
rskraba pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 5d9984e AVRO-3182 fix union schema ToString() has an extra type
property (#1303)
5d9984e is described below
commit 5d9984ef07d821d40d70341e2848a3b312798e5c
Author: ShawnWu <[email protected]>
AuthorDate: Mon Sep 6 21:23:24 2021 +0800
AVRO-3182 fix union schema ToString() has an extra type property (#1303)
* AVRO-3182 fix union schema ToString() has an extra type property
* AVRO-3182 add unit test
* AVRO-3182 fix code scanning warning to avoid introducing a dependency
cycle
* AVRO-3182 fix code scanning warning that missing dispose call on local
IDisposable
Co-authored-by: Shawn Wu <[email protected]>
---
.../csharp/src/apache/main/Schema/PrimitiveSchema.cs | 20 ++++++++++++++++++++
lang/csharp/src/apache/main/Schema/Schema.cs | 18 +++++-------------
lang/csharp/src/apache/test/Schema/SchemaTests.cs | 7 +++++++
3 files changed, 32 insertions(+), 13 deletions(-)
diff --git a/lang/csharp/src/apache/main/Schema/PrimitiveSchema.cs
b/lang/csharp/src/apache/main/Schema/PrimitiveSchema.cs
index 13485d1..1a55c2f 100644
--- a/lang/csharp/src/apache/main/Schema/PrimitiveSchema.cs
+++ b/lang/csharp/src/apache/main/Schema/PrimitiveSchema.cs
@@ -133,5 +133,25 @@ namespace Avro
{
return 13 * Tag.GetHashCode() + getHashCode(Props);
}
+
+ /// <summary>
+ /// Returns the canonical JSON representation of this schema.
+ /// </summary>
+ /// <returns>The canonical JSON representation of this
schema.</returns>
+ public override string ToString()
+ {
+ using (System.IO.StringWriter sw = new System.IO.StringWriter())
+ using (Newtonsoft.Json.JsonTextWriter writer = new
Newtonsoft.Json.JsonTextWriter(sw))
+ {
+ writer.WriteStartObject();
+ writer.WritePropertyName("type");
+
+ WriteJson(writer, new SchemaNames(), null); // stand alone
schema, so no enclosing name space
+
+ writer.WriteEndObject();
+
+ return sw.ToString();
+ }
+ }
}
}
diff --git a/lang/csharp/src/apache/main/Schema/Schema.cs
b/lang/csharp/src/apache/main/Schema/Schema.cs
index 284b574..94b96db 100644
--- a/lang/csharp/src/apache/main/Schema/Schema.cs
+++ b/lang/csharp/src/apache/main/Schema/Schema.cs
@@ -266,21 +266,13 @@ namespace Avro
/// <returns>The canonical JSON representation of this
schema.</returns>
public override string ToString()
{
- System.IO.StringWriter sw = new System.IO.StringWriter();
- Newtonsoft.Json.JsonTextWriter writer = new
Newtonsoft.Json.JsonTextWriter(sw);
-
- if (this is PrimitiveSchema || this is UnionSchema)
+ using (System.IO.StringWriter sw = new System.IO.StringWriter())
+ using (Newtonsoft.Json.JsonTextWriter writer = new
Newtonsoft.Json.JsonTextWriter(sw))
{
- writer.WriteStartObject();
- writer.WritePropertyName("type");
- }
-
- WriteJson(writer, new SchemaNames(), null); // stand alone schema,
so no enclosing name space
+ WriteJson(writer, new SchemaNames(), null); // stand alone
schema, so no enclosing name space
- if (this is PrimitiveSchema || this is UnionSchema)
- writer.WriteEndObject();
-
- return sw.ToString();
+ return sw.ToString();
+ }
}
/// <summary>
diff --git a/lang/csharp/src/apache/test/Schema/SchemaTests.cs
b/lang/csharp/src/apache/test/Schema/SchemaTests.cs
index 1d67742..7145670 100644
--- a/lang/csharp/src/apache/test/Schema/SchemaTests.cs
+++ b/lang/csharp/src/apache/test/Schema/SchemaTests.cs
@@ -360,5 +360,12 @@ namespace Avro.Test
Assert.AreEqual(expectedName, schema.Name);
}
+
+ [TestCase("[\"null\",\"string\"]", "[\"null\",\"string\"]")]
+ public void TestUnionSchemaWithoutTypeProperty(string schemaJson,
string expectedSchemaJson)
+ {
+ var schema = Schema.Parse(schemaJson);
+ Assert.AreEqual(schema.ToString(), expectedSchemaJson);
+ }
}
}