Skip to content
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
16 changes: 12 additions & 4 deletions lang/csharp/src/apache/main/Schema/EnumSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,24 @@ protected internal override void WriteJsonFields(Newtonsoft.Json.JsonTextWriter
/// Throws AvroException if the symbol is not found in this enum.
/// </summary>
/// <param name="symbol">name of the symbol to find</param>
/// <returns>position of the given symbol in this enum schema</returns>
/// <returns>
/// position of the given symbol in this enum schema
/// </returns>
/// <exception cref="Avro.AvroException">No such symbol: {symbol}</exception>
public int Ordinal(string symbol)
{
int result;
if (symbolMap.TryGetValue(symbol, out result))
{
return result;
}

if (Default != null && symbolMap.TryGetValue(Default, out result))
{
return result;
if (null != Default)
return symbolMap[Default];
}

throw new AvroException("No such symbol: " + symbol);
throw new AvroException($"No such symbol: {symbol}");
}

/// <summary>
Expand Down
24 changes: 22 additions & 2 deletions lang/csharp/src/apache/test/Schema/SchemaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,24 @@ private static void testToString(Schema sc)
}
}

private static void testToString(Schema sc, string schema)
{
try
{
//remove any excess spaces in the JSON to normalize the match with toString
schema = schema.Replace("{ ", "{")
.Replace("} ", "}")
.Replace("\" ", "\"")
.Replace(", ", ",")
.Replace(": ", ":");
Assert.AreEqual(sc.ToString(), schema);
}
catch (Exception e)
{
throw new AvroException($"{e} : {sc}", e);
}
}

[TestCase("{\"type\":\"record\",\"name\":\"LongList\"," +
"\"fields\":[{\"name\":\"f1\",\"type\":\"long\"}," +
"{\"name\":\"f2\",\"type\": \"int\"}]}",
Expand Down Expand Up @@ -231,6 +249,8 @@ public void TestRecordDoc(string s, string expectedDoc)

[TestCase("{\"type\": \"enum\", \"name\": \"Test\", \"symbols\": [\"A\", \"B\"]}",
new string[] { "A", "B" })]
[TestCase("{\"type\": \"enum\",\"name\":\"Market\",\"symbols\":[\"UNKNOWN\",\"A\",\"B\"],\"default\":\"UNKNOWN\"}",
new string[] { "UNKNOWN", "A", "B" })]
public void TestEnum(string s, string[] symbols)
{
Schema sc = Schema.Parse(s);
Expand All @@ -239,13 +259,13 @@ public void TestEnum(string s, string[] symbols)
Assert.AreEqual(symbols.Length, es.Count);

int i = 0;
foreach (String str in es)
foreach (string str in es)
{
Assert.AreEqual(symbols[i++], str);
}

testEquality(s, sc);
testToString(sc);
testToString(sc, s);
}

[TestCase("{\"type\": \"enum\", \"name\": \"Test\", \"symbols\": [\"A\", \"B\"]}", null)]
Expand Down