This is an automated email from the ASF dual-hosted git repository. blachniet pushed a commit to branch branch-1.9 in repository https://gitbox.apache.org/repos/asf/avro.git
commit fea6888bc211e76abd5823bf50d9bfb709b47ab7 Author: Brian Lachniet <[email protected]> AuthorDate: Sat Aug 24 10:34:02 2019 -0400 AVRO-2522: Fix nullable resolution inside lists (#663) (cherry picked commit from 23b6eb8a65cffbecc6166757a009644ace78f026) --- lang/csharp/src/apache/main/CodeGen/CodeGen.cs | 12 +-- .../apache/test/Specific/EmbeddedGenericsRecord.cs | 114 +++++++++++++++++++++ .../src/apache/test/Specific/SpecificTests.cs | 62 +++++++++++ 3 files changed, 182 insertions(+), 6 deletions(-) diff --git a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs index 40c7256..10197a0 100644 --- a/lang/csharp/src/apache/main/CodeGen/CodeGen.cs +++ b/lang/csharp/src/apache/main/CodeGen/CodeGen.cs @@ -703,21 +703,21 @@ namespace Avro switch (schema.Tag) { case Schema.Type.Null: - return "System.Object"; + return typeof(object).ToString(); case Schema.Type.Boolean: - if (nullible) return "System.Nullable<bool>"; + if (nullible) return $"System.Nullable<{typeof(bool)}>"; else return typeof(bool).ToString(); case Schema.Type.Int: - if (nullible) return "System.Nullable<int>"; + if (nullible) return $"System.Nullable<{typeof(int)}>"; else return typeof(int).ToString(); case Schema.Type.Long: - if (nullible) return "System.Nullable<long>"; + if (nullible) return $"System.Nullable<{typeof(long)}>"; else return typeof(long).ToString(); case Schema.Type.Float: - if (nullible) return "System.Nullable<float>"; + if (nullible) return $"System.Nullable<{typeof(float)}>"; else return typeof(float).ToString(); case Schema.Type.Double: - if (nullible) return "System.Nullable<double>"; + if (nullible) return $"System.Nullable<{typeof(double)}>"; else return typeof(double).ToString(); case Schema.Type.Bytes: diff --git a/lang/csharp/src/apache/test/Specific/EmbeddedGenericsRecord.cs b/lang/csharp/src/apache/test/Specific/EmbeddedGenericsRecord.cs new file mode 100644 index 0000000..c6595a2 --- /dev/null +++ b/lang/csharp/src/apache/test/Specific/EmbeddedGenericsRecord.cs @@ -0,0 +1,114 @@ +/* + * 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. + */ +// ------------------------------------------------------------------------------ +// <auto-generated> +// Generated by avrogen, version 1.10.0.0 +// Changes to this file may cause incorrect behavior and will be lost if code +// is regenerated +// </auto-generated> +// ------------------------------------------------------------------------------ +namespace Avro.Test.Specific +{ + using System; + using System.Collections.Generic; + using System.Text; + using Avro; + using Avro.Specific; + + public partial class EmbeddedGenericsRecord : ISpecificRecord + { + public static Schema _SCHEMA = Avro.Schema.Parse(@"{""type"":""record"",""name"":""EmbeddedGenericsRecord"",""namespace"":""Avro.Test.Specific"",""fields"":[{""name"":""OptionalInt"",""type"":[""null"",""int""]},{""name"":""OptionalIntList"",""type"":{""type"":""array"",""items"":[""null"",""int""]}},{""name"":""OptionalIntMatrix"",""type"":{""type"":""array"",""items"":{""type"":""array"",""items"":{""type"":""array"",""items"":[""null"",""int""]}}}},{""name"":""IntMatrix"",""type"":{ [...] + private System.Nullable<System.Int32> _OptionalInt; + private IList<System.Nullable<System.Int32>> _OptionalIntList; + private IList<IList<IList<System.Nullable<System.Int32>>>> _OptionalIntMatrix; + private IList<IList<IList<System.Int32>>> _IntMatrix; + public virtual Schema Schema + { + get + { + return EmbeddedGenericsRecord._SCHEMA; + } + } + public System.Nullable<System.Int32> OptionalInt + { + get + { + return this._OptionalInt; + } + set + { + this._OptionalInt = value; + } + } + public IList<System.Nullable<System.Int32>> OptionalIntList + { + get + { + return this._OptionalIntList; + } + set + { + this._OptionalIntList = value; + } + } + public IList<IList<IList<System.Nullable<System.Int32>>>> OptionalIntMatrix + { + get + { + return this._OptionalIntMatrix; + } + set + { + this._OptionalIntMatrix = value; + } + } + public IList<IList<IList<System.Int32>>> IntMatrix + { + get + { + return this._IntMatrix; + } + set + { + this._IntMatrix = value; + } + } + public virtual object Get(int fieldPos) + { + switch (fieldPos) + { + case 0: return this.OptionalInt; + case 1: return this.OptionalIntList; + case 2: return this.OptionalIntMatrix; + case 3: return this.IntMatrix; + default: throw new AvroRuntimeException("Bad index " + fieldPos + " in Get()"); + }; + } + public virtual void Put(int fieldPos, object fieldValue) + { + switch (fieldPos) + { + case 0: this.OptionalInt = (System.Nullable<System.Int32>)fieldValue; break; + case 1: this.OptionalIntList = (IList<System.Nullable<System.Int32>>)fieldValue; break; + case 2: this.OptionalIntMatrix = (IList<IList<IList<System.Nullable<System.Int32>>>>)fieldValue; break; + case 3: this.IntMatrix = (IList<IList<IList<System.Int32>>>)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 99df3bf..9da4b83 100644 --- a/lang/csharp/src/apache/test/Specific/SpecificTests.cs +++ b/lang/csharp/src/apache/test/Specific/SpecificTests.cs @@ -25,6 +25,8 @@ using System.CodeDom; using System.CodeDom.Compiler; using Avro.Specific; using System.Reflection; +using Avro.Test.Specific; +using System.Collections.Generic; namespace Avro.Test { @@ -246,6 +248,66 @@ namespace Avro.Test Assert.AreEqual( EnumType.SECOND, rec2.enumType ); } + [Test] + public void TestEmbeddedGenerics() + { + var srcRecord = new EmbeddedGenericsRecord + { + OptionalIntList = new List<int?> { 1, 2, null, 3, null, null }, + OptionalIntMatrix = new List<IList<IList<int?>>> + { + new List<IList<int?>> + { + new List<int?> { null, 2, }, + new List<int?> { null, null }, + }, + new List<IList<int?>> + { + new List<int?> { 5, 6, }, + }, + new List<IList<int?>> { }, + }, + IntMatrix = new List<IList<IList<int>>> + { + new List<IList<int>> + { + new List<int> { 1, 2, }, + new List<int> { 3, 4, }, + }, + new List<IList<int>> + { + new List<int> { 5, 6, }, + }, + new List<IList<int>> { }, + } + }; + var stream = serialize(EmbeddedGenericsRecord._SCHEMA, srcRecord); + var dstRecord = deserialize<EmbeddedGenericsRecord>(stream, + EmbeddedGenericsRecord._SCHEMA, EmbeddedGenericsRecord._SCHEMA); + + Assert.NotNull(dstRecord); + Assert.AreEqual(1, dstRecord.OptionalIntList[0]); + Assert.AreEqual(2, dstRecord.OptionalIntList[1]); + Assert.AreEqual(null, dstRecord.OptionalIntList[2]); + Assert.AreEqual(3, dstRecord.OptionalIntList[3]); + Assert.AreEqual(null, dstRecord.OptionalIntList[4]); + Assert.AreEqual(null, dstRecord.OptionalIntList[5]); + Assert.AreEqual(null, dstRecord.OptionalIntMatrix[0][0][0]); + Assert.AreEqual(2, dstRecord.OptionalIntMatrix[0][0][1]); + Assert.AreEqual(null, dstRecord.OptionalIntMatrix[0][1][0]); + Assert.AreEqual(null, dstRecord.OptionalIntMatrix[0][1][1]); + Assert.AreEqual(5, dstRecord.OptionalIntMatrix[1][0][0]); + Assert.AreEqual(6, dstRecord.OptionalIntMatrix[1][0][1]); + Assert.AreEqual(0, dstRecord.OptionalIntMatrix[2].Count); + Assert.AreEqual(1, dstRecord.IntMatrix[0][0][0]); + Assert.AreEqual(2, dstRecord.IntMatrix[0][0][1]); + Assert.AreEqual(3, dstRecord.IntMatrix[0][1][0]); + Assert.AreEqual(4, dstRecord.IntMatrix[0][1][1]); + Assert.AreEqual(5, dstRecord.IntMatrix[1][0][0]); + Assert.AreEqual(6, dstRecord.IntMatrix[1][0][1]); + Assert.AreEqual(0, dstRecord.IntMatrix[2].Count); + } + private static S deserialize<S>(Stream ms, Schema ws, Schema rs) where S : class, ISpecificRecord { long initialPos = ms.Position;
