diff --git a/lang/csharp/src/apache/main/Schema/UnionSchema.cs b/lang/csharp/src/apache/main/Schema/UnionSchema.cs index 0ffb5e091f7..a884a85965e 100644 --- a/lang/csharp/src/apache/main/Schema/UnionSchema.cs +++ b/lang/csharp/src/apache/main/Schema/UnionSchema.cs @@ -115,8 +115,21 @@ public int MatchingBranch(Schema s) { if (s is UnionSchema) throw new AvroException("Cannot find a match against union schema"); // Try exact match. - //for (int i = 0; i < Count; i++) if (Schemas[i].Equals(s)) return i; // removed this for performance's sake - for (int i = 0; i < Count; i++) if (Schemas[i].CanRead(s)) return i; + // CanRead might find a compatible schema which can read. e.g. double and long + for (int i = 0; i < Count; i++) + { + if (Schemas[i].Equals(s)) + { + return i; + } + } + for (int i = 0; i < Count; i++) + { + if (Schemas[i].CanRead(s)) + { + return i; + } + } return -1; } diff --git a/lang/csharp/src/apache/test/Generic/GenericTests.cs b/lang/csharp/src/apache/test/Generic/GenericTests.cs index 8e0a86cdf3c..b87ce69f890 100644 --- a/lang/csharp/src/apache/test/Generic/GenericTests.cs +++ b/lang/csharp/src/apache/test/Generic/GenericTests.cs @@ -128,6 +128,18 @@ private static void test(string s, T value) [TestCase("[\"int\", \"long\"]", 100L)] [TestCase("[\"float\", \"double\"]", 100.75)] [TestCase("[\"float\", \"double\"]", 23.67f)] + [TestCase("[\"float\", \"int\"]", 0)] + [TestCase("[\"float\", \"int\"]", 0.0f)] + [TestCase("[\"float\", \"int\"]", 100)] + [TestCase("[\"float\", \"int\"]", 100.0f)] + [TestCase("[\"float\", \"int\"]", -100)] + [TestCase("[\"float\", \"int\"]", -100.0f)] + [TestCase("[\"double\", \"long\"]", 0L)] + [TestCase("[\"double\", \"long\"]", 0.0)] + [TestCase("[\"double\", \"long\"]", 100L)] + [TestCase("[\"double\", \"long\"]", 100.0)] + [TestCase("[\"double\", \"long\"]", -100L)] + [TestCase("[\"double\", \"long\"]", -100.0)] [TestCase("[{\"type\": \"array\", \"items\": \"float\"}, \"double\"]", new float[] { 23.67f, 22.78f })] [TestCase("[{\"type\": \"array\", \"items\": \"float\"}, \"double\"]", 100.89)] [TestCase("[{\"type\": \"array\", \"items\": \"string\"}, \"string\"]", "a")] diff --git a/lang/csharp/src/apache/test/Specific/DoubleLongUnionRecord.cs b/lang/csharp/src/apache/test/Specific/DoubleLongUnionRecord.cs new file mode 100644 index 00000000000..97b94be7eed --- /dev/null +++ b/lang/csharp/src/apache/test/Specific/DoubleLongUnionRecord.cs @@ -0,0 +1,73 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// ------------------------------------------------------------------------------ +// +// Generated by avrogen, version 1.11.0.0 +// Changes to this file may cause incorrect behavior and will be lost if code +// is regenerated +// +// ------------------------------------------------------------------------------ +namespace Avro.Test.Specific +{ + using System; + using System.Collections.Generic; + using System.Text; + using Avro; + using Avro.Specific; + + public partial class DoubleLongUnionRecord : ISpecificRecord + { + public static Schema _SCHEMA = Avro.Schema.Parse("{\"type\":\"record\",\"name\":\"DoubleLongUnionRecord\",\"namespace\":\"Avro.Test.Specific\",\"fields\":[{\"name" + + "\":\"Property\",\"type\":[\"double\",\"long\"]}]}"); + private object _Property; + public virtual Schema Schema + { + get + { + return DoubleLongUnionRecord._SCHEMA; + } + } + public object Property + { + get + { + return this._Property; + } + set + { + this._Property = value; + } + } + public virtual object Get(int fieldPos) + { + switch (fieldPos) + { + case 0: return this.Property; + default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()"); + }; + } + public virtual void Put(int fieldPos, object fieldValue) + { + switch (fieldPos) + { + case 0: this.Property = (System.Object)fieldValue; break; + default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Put()"); + }; + } + } +} diff --git a/lang/csharp/src/apache/test/Specific/SpecificTests.cs b/lang/csharp/src/apache/test/Specific/SpecificTests.cs index 1ba6840d45c..5a7e7bc2921 100644 --- a/lang/csharp/src/apache/test/Specific/SpecificTests.cs +++ b/lang/csharp/src/apache/test/Specific/SpecificTests.cs @@ -272,6 +272,38 @@ public void TestEnumDefault() Assert.AreEqual(EnumType.DEFAULT, rec2.enumType); } + [TestCase(0L)] + [TestCase(100L)] + [TestCase(-100L)] + [TestCase(0.0)] + [TestCase(100.0)] + [TestCase(-100.0)] + public void TestDoubleLongUnion(object value) + { + var testRecord = new DoubleLongUnionRecord(); + testRecord.Property = value; + + // serialize + var stream = serialize(DoubleLongUnionRecord._SCHEMA, testRecord); + + // deserialize + var rec2 = deserialize(stream, DoubleLongUnionRecord._SCHEMA, DoubleLongUnionRecord._SCHEMA); + Assert.AreEqual(value, rec2.Property); + Assert.AreEqual(value.GetType(), rec2.Property.GetType()); + } + + [TestCase(0)] + [TestCase(100)] + [TestCase(-100)] + [TestCase(0.0f)] + [TestCase(100.0f)] + [TestCase(-100.0f)] + [TestCase("0")] + [TestCase("100")] + public void TestDoubleLongUnionNoMatchException(object value) + { + Assert.Throws(() => serialize(DoubleLongUnionRecord._SCHEMA, new DoubleLongUnionRecord() { Property = value })); + } [Test] public void TestArrayWithReservedWords()